Mar 12, 2010 at 3:10am UTC
Trying to write my own complex number class, when I overload the << operator as below, it compiles fine when I don't try to use the << operator, but when I try to use the operator in main I get this error in VS 2009:
1>------ Build started: Project: complex, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : 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 complex<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$complex@H@@@Z) referenced in function _main
Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
template <typename T>
class complex{
public :
//constructors etc.
friend std::ostream& operator <<(ostream&, const complex<T>&);
private :
T Re, Im;
};
template <typename T>
std::ostream& operator <<(std::ostream &out, const complex<T> &rhs){
out << (double )rhs.Re << " " << (double )rhs.Im;
return out;
}
int main(){
complex num(3,4);
cout << num << endl;
return 0;
}
Last edited on Mar 12, 2010 at 3:16am UTC
Mar 12, 2010 at 3:17am UTC
You don't seem to be including iostream.
Mar 12, 2010 at 3:18am UTC
I didn't think it was necessary to show that in this post, but in the actual code I am including iostream.
Mar 12, 2010 at 3:18am UTC
The function you're declaring on line 5 is not templated. Add template <typename T2> before it, and change <T> to <T2>.
Mar 12, 2010 at 3:23am UTC
Wow stupid mistake, thanks.