I"m working on a cPP template problem, described as follows:
"create a template for the Array class. This template allows the Array object to instantiate a specific element type that specifies the number of elements at compile time. "
and the compilation always fails when I overload the input and output stream functions.
the compiler shows:
[Warning] friend declaration "std::istream& operator > > (std::istream&, Array < T, n > &)" declares a non-template function [- Wnon-template-friend]
[Warning] friend declaration "std::ostream& operator < < (std::ostream&, const Array < T, n > &)" declares a non-template function [- Wnon-template-friend]
the code is as follows:
-sharpinclude<iostream>
using namespace std;
template<class T,int n>
class Array
{
private:
T p[n];
static int count;
public:
friend istream & operator>> (istream & in, Array<T,n>& a);
friend ostream & operator << (ostream & out,const Array<T,n>& a);
int getSize()
{
return n;
}
static int getArrayCount()
{
return count;
}
};
template<class T,int n>
istream & operator >> (istream & in,const Array<T,n>& a)
{
for(int i=0;i<n;iPP)
{
in>>a.p[i];
}
a.countPP;
return in;
}
template<class T,int n>
ostream & operator << (ostream & out,const Array<T,n>& a)
{
for(int i=0;i<n;iPP)
{
out<<a.p[i]<<" ";
}
return out;
}
paste the main function here:
int main()
{
Array< int, 5 > intArray1;
cin >> intArray1;
Array< int, 5 > intArray2;
cin >> intArray2;
Array< float, 5 > floatArray;
cin >> floatArray;
cout << "\nIntArray1 contains " << intArray1.getSize() << " Elements.\n";
cout << "The values in intArray are:\n";
cout << intArray1;
cout << "\nIntArray2 contains " << intArray2.getSize() << " Elements.\n";
cout << "The values in intArray are:\n";
cout << intArray2;
cout << "\nDoubleArray contains " << floatArray.getSize() << " Elements.\n";
cout << "The values in the doubleArray are:\n";
cout << floatArray;
cout << "\nThere are " << Array<int,5>::getArrayCount() << " Array<int,5> objects.\n";
cout << "\nThere are " << Array<float,5>::getArrayCount() << " Array<float,5> objects.\n";
return 0;
}
I have searched the Internet for a long time and tried a lot of solutions, but neither adding < > after > > in the line
istream & operator > (istream & in,const Array < TMagazn > & a)
nor defining input and output functions in the class does not work.
if defined in a class, the following error appears:
undefined reference to `Array < float, 5 >:: count"
[Error] ld returned 1 exit status
if you add < > after > >, the following error appears:
[Error] template-id "operator > < >" in declaration of primary template
so it"s hard to walk now, and I don"t have a clue at all.
I also hope that all the great gods can answer my doubts and explain why the above three mistakes occurred.
Thank you very much!