1

I'm writing a program that creates a complex number class and I am getting these two errors when i try to test my overloaded operator >>. Here are the errors:

error LNK2028: unresolved token (0A0002BD) "class std::basic_istream > & __cdecl operator>>(class std::basic_istream > &,class Complex const &)" (??5@$$FYAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@ABVComplex@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

error LNK2019: unresolved external symbol "class std::basic_istream > & __cdecl operator>>(class std::basic_istream > &,class Complex const &)" (??5@$$FYAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@ABVComplex@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

Here is my overload function:

istream& operator >> (istream& in, Complex& a){
    double real, imaginary;
    in >> real >> imaginary;
    a.setReal(real);
    a.setImaginary(imaginary);  
    return in;
}

Also it says its coming from my mainComplex.obj, mainComplex is a cpp file that has the main function i use to test the program.

int main(){     
    Complex num;
    cout << "Enter Complex number: ";
    cin >> num;
    return 0;
}
Thomas
  • 99
  • 4
  • 11

2 Answers2

4

The compiler, when processing main has found that the best overload for the expression cin >> num; is std::basic_istream<...>& operator>>( std::basic_istream<...>&, const Complex& ), note the const in the second argument.

This probably indicates that you have declared the operator as:

std::istream& operator>>( std::istream&, const Complex & );

But then implemented:

std::istream& operator>>( std::istream&, Complex & );
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Ah yes! It compiles and works! Thank you very much! I guess i just overlooked that const in the declaration. – Thomas May 21 '11 at 23:14
  • Error strings are not the simplest to read, but they contain information on what the compiler/linker are interpreting from your code, and that does help catching errors. – David Rodríguez - dribeas May 21 '11 at 23:16
0

I'm guessing you have implemented the >> operator as a member of your class when really it needs to be a friend in order to use it in the way you intend.

Have a look at this SO question: Should operator<< be implemented as a friend or as a member function?

There is also a section on these operators in Scott Meyers: Effective C++ Programming but I don't have the book to hand to give a reference.

Community
  • 1
  • 1
Tony
  • 9,672
  • 3
  • 47
  • 75
  • My overloaded operators are all declared as friend functions in my Complex class not as member functions. – Thomas May 21 '11 at 22:59
  • In which case could it be you are trying to read from `cin` within your function, rather than from the istream parameter `in`: `cin >> real >> imaginary;` should that line be `in >> real >> imaginary;` – Tony May 21 '11 at 23:03
  • Thanks for catching that, but it still gives me the same errors when I compile. – Thomas May 21 '11 at 23:07