Template issue

Hi, i've got a little problem with templates. I got many errors when i try to compile this peace of code:
1
2
3
4
5
6
7
8
9
10
template <class T>
class CNumber
{
public:
	CNumber(void):m_Number(43){}
	~CNumber(void);
	friend ostream & operator << (ostream &, CNumber<T> &);
private:
	T m_Number;
};


Any ideas what's wrong?
First error is:

error C2143: syntax error : missing ';' before '&'
try std::ostream
It works. Thanks brotha.

I've got another problem. I'm getting error again, when i try to compile my program.

1
2
3
4
5
6
7
8
9
10
11
//Number.h
template <class T>
class CNumber
{
public:
	CNumber(void):m_Number(43){}
	~CNumber(void);
	friend std::ostream & operator << (std::ostream &, CNumber<T> &);
private:
	T m_Number;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "Number.h"

using namespace std;

template <class T>
ostream & operator << (ostream &, CNumber<T> &);

int _tmain(int argc, _TCHAR* argv[])
{
	CNumber<int> number;
	cout<<number<<endl;

	system("pause");
	return 0;
}

template <class T>
ostream & operator << (ostream & o, CNumber<T> & num)
{
	return o<<"Number="<<num.m_Number;
}
Last edited on
Make the friend declaration a template:
1
2
template <class TT> // TT so it's not confused with the class template T
        friend std::ostream & operator << (std::ostream &, CNumber<TT> &); // same here 
It still doesn't work. I'm getting this error:

error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CMArray<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$CMArray@H@@@Z) referenced in function _wmain
//Edit
I got this. I add:

template <class TT>

before friendship declaration in .h file. Now it works. I appreciate your help. Thanks.
Topic archived. No new replies allowed.