简体   繁体   English

模板类的好友函数:C ++

[英]friend function for template class : C++

I have written a code: 我写了一个代码:

#include <iostream>
#include <cassert>

using namespace std;

template<typename T>
class dynArray{
    int size;
    T* ptr;
    public:
    dynArray(int n=0);
    ~dynArray();
    T& operator[] (const int index);
    friend ostream& operator<<<T>(ostream& os,const dynArray<T>& A);
    };

template<typename T> dynArray<T>::dynArray(int n):size(n){
if (size==0)
{
 cout << "Size Zero"<< endl;
 ptr=NULL;}
 else
 {
     try{
         ptr = new T[size];
         cout << "Constructor Called" << endl;
         }
     catch(bad_alloc xa)
     {
         cout << "Allocation Failure" <<endl;
         exit(EXIT_FAILURE);
         }
     }
}

template<typename T> dynArray<T>::~dynArray(){
    cout << "Destructor Called for array of size : " << size << endl;
    delete[] ptr;
    }

template<typename T> T& dynArray<T>::operator[] (const int index){
    assert(index >=0 && index <size);
    return *(ptr+index);
    }

template<typename T> ostream& operator<< <T>(ostream& os,const dynArray<T>& A){
    for (int i=0; i < A.size ; i++)
    os << *(A.ptr+i) << " ";
    return os;
    }

int main()
{
    dynArray<int> array1;
    dynArray<int> array2(5);

    array2[0]=15;
    array2[3]= 45;


    cout << array2 << endl;

    return 0;
}

But while compiling getting error: 但是在编译时出现错误:

$ g++ -Wall DynArray.cpp -o DynArray
DynArray.cpp:46: error: partial specialization `operator<< <T>' of function template
DynArray.cpp: In instantiation of `dynArray<int>':
DynArray.cpp:54:   instantiated from here
DynArray.cpp:14: error: template-id `operator<< <int>' for `std::basic_ostream<char, std::char_traits<char> >& operator<<(std::basic_ostream<char, std::char_traits<char> >&, const dynArray<int>&)' does not match any template declaration
DynArray.cpp: In function `std::ostream& operator<<(std::ostream&, const dynArray<T>&) [with T = int]':
DynArray.cpp:61:   instantiated from here
DynArray.cpp:8: error: `int dynArray<int>::size' is private
DynArray.cpp:47: error: within this context
DynArray.cpp:9: error: `int*dynArray<int>::ptr' is private
DynArray.cpp:48: error: within this context

I think my template syntax is wrong for operator<< . 我认为operator<<模板语法错误。 Can any one help please? 有人可以帮忙吗? Not able to figure out where is the mistake. 无法找出错误所在。

You cannot friend a specialization of an undeclared template function. 您不能将未声明的模板函数的专业化作为friend You need to declare the operator << beforehand. 您需要事先声明operator << Of course to do so, you'll need to already have a declaration of dynArray . 当然,您需要已经有了dynArray的声明。 This mess of forward declarations looks like this ( Live at Coliru ): 这些混乱的前向声明看起来像这样( Live at Coliru ):

template<typename T> class dynArray;

template<typename T>
ostream& operator << (ostream& os, const dynArray<T>& A);

template<typename T>
class dynArray {
    // ...
    friend ostream& operator<< <T>(ostream& os, const dynArray& A);
    };

As @ooga points out in his comment , the template parameters in the friend declaration are unnecessary; 正如@ooga在其评论中指出的那样,朋友声明中的模板参数是不必要的; the compiler can deduce them. 编译器可以推断出它们。 You could simply declare it as: 您可以简单地将其声明为:

friend ostream& operator<< <> (ostream& os, const dynArray& A);

I personally find that syntax a bit punctuation-heavy in this instance. 我个人发现这种情况下的语法有点标点符号。

Alternatively, if you find the forward declarations offensive, you could declare a separate non-template friend function for each specialization of dynArray by defining it in the class definition ( Coliru again ): 另外,如果你发现正向声明进攻,你可以声明为每个专业化的独立非模板友元函数dynArray由类定义定义它( Coliru再次 ):

template<typename T>
class dynArray {
    // ...
    friend ostream& operator<< (ostream& os,const dynArray& A) {
        for (int i=0; i < A.size ; i++)
        os << *(A.ptr+i) << " ";
        return os;
    }
};

In the class declare friend as :- 在课堂上宣布好友为:-

template<typename U>
friend ostream& operator<<(ostream& os,const dynArray<U>& A);

And define it as :- 并将其定义为:

template<typename U> 
ostream& operator<< (ostream& os,const dynArray<U>& A){...}

See HERE 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM