提问者:小点点

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


我有一个类模板,它将输出存储在数组中的对象列表。 我得到以下错误,我搞不清错误是在什么地方引起的,因为错误是在。obj和。exe文件中。

1未解析的外部(proj08.exe第1行)
未解析的外部符号“class std::basic_ostream>&__cdecl operator<<(class std::basic_ostream>&,class MyVector)”(??6@yaav?$basic_ostream@du?$char_traits@d@std@@aav01@v?$MyVector@n@@@z)在函数_main中引用(Porj08.obj第1行)

项目08.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;
}

共2个答案

匿名用户

如果要匹配定义的函数模板,则必须将friend声明为函数模板:

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

如果为类模板声明了friend函数,则不会使其成为函数模板。

匿名用户

@LogicStuff的答案完全有效。 我想澄清一下在您的代码中到底发生了什么,以及如何使用替代方法来避免它,因为我相信大多数C++程序员至少遇到过一次这个问题。 基本上,当模板被实例化时,比如说

MyVector<int> something;

然后friend声明自动绑定到模板类型,在本例中为int,因此编译器生成

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

然而,这只是一个宣言。 你的后一个定义

template<class T>
ostream& operator << (ostream& os, MyVector<T> vt)

int的匹配没有任何关系,因为前者是更好的匹配

cout << something;

编译器尝试调用int版本(没有定义)。 因此会出现链接器错误。

一种广泛使用的替代方法是在类中内联定义运算符,如

friend 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;
}

现在,您的MyVect的每个实例化都生成一个绑定到相应模板类型的运算符<<<的有效定义。 请注意,运算符本身不是成员函数,并且它仅通过参数相关查找(ADL)在全局命名空间中可见。 这个技巧被称为朋友姓名注入,在巴顿-纳克曼技巧中广泛使用,并且你能够成功地使用它,因为在像这样的呼叫中

cout << something;

调用转换为

operator<< (std::cout, something)

并且由于某事myvector类型,所以通过ADL找到运算符<<的定义。 如果运算符<<<将例如int作为第二个参数,则无法通过ADL找到它,因为基本类型没有关联的名称空间。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++-|何为|类|模板|声明|函数|模板|友|元)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?