Archives
D Programming
DD.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<<
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
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: Nov 01 2002
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
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); Nov 01 2002
Ahhh. ambiguous definition in template<class T>.template<class U> friend ostream& operator<<(ostream& os, const Output<U>& r); Nov 02 2002
|