-
tricky stuff...
Ok, so this is the continuing saga of porting my code from VC6.0 to VC7.1...
so here's the story... i have some libraries that i have rebuilt with no problems under VC7.1... now i have another project that uses this library and when i come to build it, i get 2 linker errors:
>>>
ashlibd.lib(ImagedTarget.obj) : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl ashlib::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class ashlib::Tuple2<float>)" (??6ashlib@@YAAAV?$basic_ostream@DU?$char_traits@D @std@@@std@@AAV12@V?$Tuple2@M@0@@Z) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl ashlib::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class ashlib::ImagedTarget const &)" (??6ashlib@@YAAAV?$basic_ostream@DU?$char_traits@D @std@@@std@@AAV12@ABVImagedTarget@0@@Z)
ashlibd.lib(Target.obj) : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl ashlib::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class ashlib::Tuple3<float>)" (??6ashlib@@YAAAV?$basic_ostream@DU?$char_traits@D @std@@@std@@AAV12@V?$Tuple3@M@0@@Z) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl ashlib::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class ashlib::Target const &)" (??6ashlib@@YAAAV?$basic_ostream@DU?$char_traits@D @std@@@std@@AAV12@ABVTarget@0@@Z)
>>>
now, i have tried everything i can think of, and i really can't figure it out, but the linker errors are refering to the overloaded operator '<<' in my Tuple2 and Tuple3 classes:
Code:
#ifndef TUPLE2_H
#define TUPLE2_H
#include <cmath>
#include <fstream>
#include <iostream>
namespace ashlib {
template<class T> class Tuple2 {
public:
T x;
T y;
public:
Tuple2() { x=y=0; }
...
friend std::ostream& operator<<(std::ostream& os, Tuple2<T> v);
};
...
template<class T> std::ostream& operator<<(std::ostream& os, Tuple2<T> v) {
return os << "[" << v.x << "," << v.y << "]";
}
}//end namespace
#endif
so, there is this issue of the method not being a member function, due to the 'friend'... so maybe thats the issue... but i can't figure out how else to do it.... the annoying thing is that this all works fine under VC6.0 ...... ARRGGGGHHH!!!!!! please help.
-
i think it is something to do with this:
http://msdn.microsoft.com/library/de...rorlnk2019.asp
but i can't figure out what to do to correct it.
-
To resolve, according to this article, you would do:
friend std::ostream& operator<< <T>(std::ostream& os, Tuple2<T> v);
-
yeah, i tried that, but then the library won't compile:
>>>
c:\dev\dev\res\ASHLIB\src\ashlib\geometry\Tuple3.h (67) : error C2143: syntax error : missing ';' before '<'
c:\dev\dev\res\ASHLIB\src\ashlib\geometry\Tuple3.h (68) : see reference to class template instantiation 'ashlib::Tuple3<T>' being compiled
c:\dev\dev\res\ASHLIB\src\ashlib\geometry\Tuple3.h (67) : error C2433: 'ashlib::operator`<<'' : 'friend' not permitted on data declarations
c:\dev\dev\res\ASHLIB\src\ashlib\geometry\Tuple3.h (67) : error C2530: 'ashlib::operator`<<'' : references must be initialized
c:\dev\dev\res\ASHLIB\src\ashlib\geometry\Tuple3.h (67) : error C2238: unexpected token(s) preceding ';'
c:\dev\dev\res\ASHLIB\src\ashlib\geometry\Tuple3.h (80) : error C2904: '<<' : name already used for a template in the current scope
>>>
perhaps because its an overloaded operator? i don't know.... has anyone else run into this before?
-
i have found the solution:
Code:
namespace ashlib {
template <class T> class Tuple2;
template <class T> std::ostream& operator << (std::ostream&, Tuple2<T>);
template<class T> class Tuple2 {
public:
T x;
T y;
public:
Tuple2() { x=y=0; }
...
friend std::ostream& operator<< <>(std::ostream& os, Tuple2<T> v);
};
...
template<class T> std::ostream& operator<<(std::ostream& os, Tuple2<T> v) {
return os << "[" << v.x << "," << v.y << "]";
}
}//end namespace
#endif
-
Nice one. Have to remember that, I'm sure I'll eventually run across that problem too.