Reputation: 43
I got confused in overloading the ostream operator<< for my template class. (unnecessary code deleted)
sparseArray2D.h:
#include <iostream>
using namespace std;
template <typename T>
class sparseArray2D
{
private:
//...
public:
//...
friend ostream& operator << (ostream&, const sparseArray2D<T>&);
//...
}
template <typename T>
ostream& operator << (ostream& os, const sparseArray2D<T> &_matrix)
{
//...
os<<"Overloaded operator works";
return os;
};
and main:
#include "sparseArray2D.h"
int _tmain(int argc, _TCHAR* argv[])
{
//...
sparseArray2D<int> *matrX = new sparseArray2D<int>(10, 9, 5);
cout << matrX;
//...
}
No errors and no warnings in VS2012, but in the console I have 8 symbols as link or pointer at object. Like "0044FA80".
What's going wrong?
Upvotes: 0
Views: 521
Reputation: 304132
That's because you're overloading (not reloading) on sparseArray2D<T>
, but that is not what matrX
is:
sparseArray2D<int> *matrX = new sparseArray2D<int>(10, 9, 5);
// ^^
cout << matrX;
matrX
is a pointer. As such, you're just streaming the pointer - which by default logs its address... which is apparently 0x0044FA80.
What you want is:
cout << *matrX;
Upvotes: 2