Thomas
Thomas

Reputation: 109

Any idea why i'm getting these two errors in my c++ program?

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;
}

Upvotes: 1

Views: 307

Answers (2)

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 & );

Upvotes: 4

Tony
Tony

Reputation: 10327

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.

Upvotes: 0

Related Questions