www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - template class operator<<

↑ ↓ ← 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
↑ ↓ 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
↑ ↓ 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
↑ ↓ 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
↑ ↓ → 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