www.digitalmars.com         C & C++   DMDScript  

c++ - template class operator<<

reply Richard <fractal clark.net> writes:
Hi!

Sorry to bother with so many questions.. And thanks for all the help.

This ones about a friend operator<< and template. The linker is saying it can't
find the template operator<< definition. Ok, I do:

#include <iostream>
template<class T>
class Output {
public:
Output() { val = 0; }
Output(const Output<T>& t) : val(t.val) { }
private:
int val;
friend ostream& operator<<(ostream& os, const Output<T>& r);
};
template<class T>
ostream& operator<<(ostream& os, const Output<T>& r) {
os << r.val;
return os;
}
void main(int argc, char *argv[]) {
Output<int> v1;
cout << "v1:: " << v1;
}

And the compiler say:

sc TemplateTest.cpp -cpp -mn -C -WA -S -3 -a8 -c -gf -D_CONSOLE=1
-D_STLP_NO_NEW_IOSTREAMS=1 -oTemplateTest.obj 
link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:mainCRTStartup /BAS:4194304 /A:512
 TemplateTest.LNK 
Error: D:\APPS\DM\TEMPLATETEST\TemplateTest.OBJ(TemplateTest)  (2075292): Symbol
Undefined ??6 YAAAVostream  AAV0 ABV?$Output H   Z (ostream &cdecl <<(ostream
&,Output<int > const &))
Lines Processed: 1577  Errors: 1  Warnings: 0
Build failed

Richard
Nov 01 2002
parent reply Richard <Richard_member pathlink.com> writes:
duh.. must be getting drunk from too much work.. like previous post dude.

This ones about a friend operator<< and template. The linker is saying it can't
find the template operator<< definition. Ok, I do:
Richard
Nov 01 2002
parent reply Richard <fractal clark.net> writes:
Wait..

My post is related to the "Template Friend Member" post, but is different. So
I'm still wondering why the linker not liking the following set of instructions.
The compiler seems to have enough information on hand (the template class, the
template function, a defined template object and proper permissions). So what's
up? Something wrong with my settings? Can anyone else get this to work? Here's
the example again:

#include <iostream>

template<class T>
class Output {
public:
Output() { val = 0; }
Output(const Output<T>& t) : val(t.val) { }
private:
int val;
friend ostream& operator<<(ostream& os, const Output<T>& r);
};

template<class T>
ostream& operator<<(ostream& os, const Output<int>& r) {
os << r.val;
return os;
}

void main(int argc, char *argv[]) {
Output<int> v1;
cout << "v1:: " << v1;
}
Nov 01 2002
next sibling parent reply Christof Meerwald <cmeerw web.de> writes:
On Fri, 1 Nov 2002 20:23:37 +0000 (UTC), Richard wrote:
 #include <iostream>
 
 template<class T>
 class Output {
 public:
 Output() { val = 0; }
 Output(const Output<T>& t) : val(t.val) { }
 private:
 int val;
 friend ostream& operator<<(ostream& os, const Output<T>& r);
should be: template<class U> friend ostream& operator<<(ostream& os, const Output<U>& r);
 };
 
 template<class T>
 ostream& operator<<(ostream& os, const Output<int>& r) {
should be: template<class U> ostream& operator<<(ostream& os, const Output<U>& r) {
 os << r.val;
 return os;
 }
 
 void main(int argc, char *argv[]) {
 Output<int> v1;
 cout << "v1:: " << v1;
 }
You have to be really careful with these things... bye, Christof -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Nov 01 2002
parent Richard <fractal clark.net> writes:
Ahhh. ambiguous definition in template<class T>.

template<class U> friend ostream& operator<<(ostream& os, const Output<U>& r);
mmm.. yes I see now. U> friend ;)
template<class U>
ostream& operator<<(ostream& os, const Output<U>& r) {
Yup. Linker loves me again.
You have to be really careful with these things...
And I thank you very much. I am so very happy when it turnes out that things just work and I'm just not being careful. I was a little worried about using DM for some of my high risk projects. But I have to say that working in DM has been great! By the way, did you vote for DM in the free country survey? http://www.thefreecountry.com/developercity/ccompilerpoll.shtml Again, thanks very much, Richard
Nov 02 2002
prev sibling parent Irena <slipov100 yahoo.com> writes:
Hi ,
I have such a problem:
template <class T> friend ostream& operator<< (ostream& out, const
container<T>& c);//the definition of <<
in cpp implimentation:
template <class T>
ostream& operator<< (ostream& out, const container<T>& c)
{
	elem<T> *Iter = c.head;
	while(Iter != NULL)
	{
		out<<Iter->mData<<" ";
		Iter = Iter -> next;
	}
	out<<endl;
	return out;
}
and get the error:
C++\Container\container.cpp(110) : error C2562: '<<' : 'void'
function returning a value
Feb 22 2008