简体   繁体   中英

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<< . Can any one help please? Not able to figure out where is the mistake.

You cannot friend a specialization of an undeclared template function. You need to declare the operator << beforehand. Of course to do so, you'll need to already have a declaration of dynArray . This mess of forward declarations looks like this ( 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; 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 ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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