C++ - 如何为类模板声明函数模板友元

标签 c++ templates operator-overloading ostream

我有一个类模板,它将输出存储在数组中的对象列表。我收到以下错误,由于错误在 .obj 和 .exe 文件中,所以我很困惑错误是在哪里引起的。

1 unresolved externals (proj08.exe line 1)
unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class MyVector)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@V?$MyVector@N@@@Z) referenced in function _main (porj08.obj line 1)

proj08.cpp

#include "stdafx.h"
#include <string>
#include "MyVector.h"

const double FRACTION = 0.5; 

int main()
{
    cout << "\nCreating a vector of doubles named Sam\n";
    MyVector<double> sam;

    cout << "\nPush 12 values into the vector.";
    for (int i = 0; i < 12; i++)
        sam.push_back(i + FRACTION);

    cout << "\nHere is sam: ";
    cout << sam;
    cout << "\n---------------\n";

    cout << "\nCreating an empty vector named joe";
    MyVector<double> joe;

    // test assignment
    joe = sam;

    cout << "\nHere is joe after doing an assignment:\n ";
    cout << joe;
    cout << "\n---------------\n";

    // test the copy constructor
    MyVector<double> bill = sam;

    cout << "\nHere is bill after creating it using the copy constructor:\n ";
    cout << bill;
    cout << "\n---------------\n";

    cout << endl;
    system("PAUSE");
    return 0;
}

MyVector.h

#pragma once
#include <iostream>
#include "stdafx.h"

using namespace std;

template <class T>
class MyVector
{

private:
    int vectorSize;
    int vectorCapacity;
    T *vectorArray;

public:
    MyVector() {
        vectorArray = new T[10];
    }
    T size();
    T capacity();
    void clear();
    void push_back(T n);
    T at(int n);

    friend ostream& operator<<(ostream& os, MyVector<T> vt);

    MyVector<T> operator=(MyVector<T>&);
};

/*
 * TEMPLATE FUNCTIONS
 */

//Return array size
template<class T>
T MyVector<T>::size()
{
    return vectorSize;
}

// Return array capacity
template<class T>
T MyVector<T>::capacity()
{
    return vectorCapacity;
}

// clear array values
template<class T>
void MyVector<T>::clear()
{
    for (int i = 0; i < vectorSize; i++)
    {
        vectorArray[i] = '\0';
    }

    vectorSize = 0;
    vectorCapacity = 2;
}


// Add number to array and double array size if needed
template<class T>
void MyVector<T>::push_back(T n)
{
    int test = 100;
    if (vectorCapacity > vectorSize)
    {
        vectorArray[vectorSize] = n;
        vectorSize++;

    }
    else {

        if (vectorCapacity == 0) {
            vectorArray = new T[4];
            vectorArray[0] = n;
            vectorCapacity = 4;
            vectorSize++;
        }
        else {

            int newCapacity = vectorCapacity * 2;

            // Dynamically allocate a new array of integers what is somewhat larger than the existing array.An algorithm that is often used is to double the size of the array.

            int *tempArray = new int[newCapacity];

            // Change capacity to be the capacity of the new array.

            vectorCapacity = newCapacity;

            // Copy all of the numbers from the first array into the second, in sequence.

            for (int i = 0; i < MyVector::size(); i++)
            {
                tempArray[i] = vectorArray[i];
            }

            delete[] vectorArray;
            vectorArray = new T[newCapacity];

            for (int i = 0; i < MyVector::size(); i++)
            {
                vectorArray[i] = tempArray[i];
            }

            delete[] tempArray;

            // Add the new element at the next open slot in the new array.

            vectorArray[vectorSize] = n;

            // Increment the size;

            vectorSize++;

        }
    }
}

// Return Value and given point in array
template<class T>
T MyVector<T>::at(int n)
{
    return vectorArray[n];
}

// Set one vector to equil another
template<class T>
MyVector<T> MyVector<T>::operator=(MyVector<T>& right) {

    if (vectorCapacity < right.vectorCapacity) {
        if (vectorCapacity != 0)
            delete[] vectorArray;
        vectorArray = new T[right.vectorCapacity];
        vectorCapacity = right.vectorCapacity;
    }
    vectorSize = right.size();

    // Assign values from left to right
    for (int i = 0; i < vectorSize; i++)
    {
        vectorArray[i] = right.at(i);
    }

    return *this;
}

// Cout Vector
template<class T>
ostream& operator << (ostream& os, MyVector<T> vt)
{
    T size = vt.size();

    for (T i = 0; i < size; i++) {

        os << "index " << i << " is " << vt.at(i) << endl;

    }

    return os;
}

最佳答案

如果你想匹配你定义的那个,你必须将 friend 声明为一个函数模板:

template <typename U> // use U, so it doesn't clash with T
friend ostream& operator<<(ostream& os, MyVector<U> vt);

如果为类模板声明了一个 friend 函数,则它不会成为函数模板。

关于C++ - 如何为类模板声明函数模板友元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33861151/

相关文章:

c++ - 在模板参数推导过程中丢失限定符

c++ - C++中的默认构造函数如何初始化变量

c++ - 为什么删除操作符必须是静态的?

c++ - C++ 中 + 的运算符重载

带有模板、运算符重载和 boost::bind 的 C++ - 这个小代码有什么作用?

c++析构子类中的段错误

C++ 禁用忙碌/等待光标

c++ - 在 C++ 中选择重载模板函数时的优先级

c++ - 如何在 C++ 中将 chrono::seconds 转换为 HH:MM:SS 格式的字符串?

c++ - 动态分配的 int 数组的共享指针触发了断点