Compilation Problem
-
#include
using namespace std;
template class Array {
public:
friend ostream& operator<< (ostream&, Array&);private:
T *pType;
};template
ostream& operator<< (ostream& output, Array& theArray)
{
output << "output array" << endl;
return output;
}
ostream& operator<< (ostream&, Array&);
int main()
{
Array theArray;
cout << theArray << endl;
return 0;
}I tried compiling the following code and I get a warning on line 5 that this statement declares a non-template function but despite this warning the code does compile. However, it does not link. The error message from the linker is: /tmp/ccIbnsNk.o:t2.C:(.text+0x9b): undefined reference to `operator<<(std::basic_ostream >&, Array&)' I feel the above code defines the function the linker says is undefined. What am I missing? Bob
-
#include
using namespace std;
template class Array {
public:
friend ostream& operator<< (ostream&, Array&);private:
T *pType;
};template
ostream& operator<< (ostream& output, Array& theArray)
{
output << "output array" << endl;
return output;
}
ostream& operator<< (ostream&, Array&);
int main()
{
Array theArray;
cout << theArray << endl;
return 0;
}I tried compiling the following code and I get a warning on line 5 that this statement declares a non-template function but despite this warning the code does compile. However, it does not link. The error message from the linker is: /tmp/ccIbnsNk.o:t2.C:(.text+0x9b): undefined reference to `operator<<(std::basic_ostream >&, Array&)' I feel the above code defines the function the linker says is undefined. What am I missing? Bob
This line:
ostream& operator<< (ostream&, Array<int>&);
declares a new function that takes an Array<int> as an argument, but you haven't provided a function body which leads to the linker error. If you meant to explicitly instantiate the template for int, you need a bit more:
template ostream& operator<< (ostream&, Array<int>&);
-
This line:
ostream& operator<< (ostream&, Array<int>&);
declares a new function that takes an Array<int> as an argument, but you haven't provided a function body which leads to the linker error. If you meant to explicitly instantiate the template for int, you need a bit more:
template ostream& operator<< (ostream&, Array<int>&);
Thanks for the response. However, I added the line you suggested and it did not solve the problem. I believe the line you suggested tells the compiler that we want to specialize the implementation for the special case of int. However, I am not sure of my facts on this one. Bob
-
Thanks for the response. However, I added the line you suggested and it did not solve the problem. I believe the line you suggested tells the compiler that we want to specialize the implementation for the special case of int. However, I am not sure of my facts on this one. Bob
Sorry, I missed out that you have to fix the friend declaration too:
template<T>
friend ostream& operator<< (ostream&, Array<T>&);An explicit specialization would look like this:
template<>
ostream& operator<< (ostream& output, Array<int>& theArray)
{
output << "output int array" << endl;
return output;
}