0

I am trying to overload the output and input stream operator but get this error when compiling

Error 1 error LNK2019: unresolved external symbol "class std::basic_istream > & __cdecl operator>>(class std::basic_istream

&,class Complex const &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@ABVComplex@@@Z) referenced in function _main C:\Users\owner\Documents\Personal\practice\main.obj practice

Basically I am trying to read a user input and parse it as real and imaginary numbers

Complex.h

#pragma once
#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(void);
    ~Complex(void);
    friend ostream& operator<<(ostream&, const Complex &C);
    friend istream& operator>>(istream&, const Complex &C);
    int real;
    int imaginary;
};

Complex.cpp

#include "Complex.h"

using namespace std;

Complex::Complex()
{
    real = 0;
    imaginary = 0;
}


Complex::~Complex(void)
{
}

ostream &operator<<(ostream &output, const Complex &C)
{
    char symbol = '+';
    if (C.imaginary < 0)
        symbol = '-';
    output << C.real << ' ' << symbol << ' ' << abs(C.imaginary) << 'i';
    return output;
}

istream &operator>>(istream &input, Complex &C)
{
    int tempReal;
    char symbol;
    char tempImaginaryFull[2];
    int tempImaginary;
    input >> tempReal >> symbol >> tempImaginaryFull;
    C.real = tempReal;
    tempImaginary = tempImaginaryFull[0];
    C.imaginary = tempImaginary;
    if( symbol == '-')
        C.imaginary *= -1;

    return input;
}

I don't really know what that error means even though I've tried looking around for it.

jophab
  • 5,356
  • 14
  • 41
  • 60
clifford.duke
  • 3,980
  • 10
  • 38
  • 63
  • whats your compile line? – Anycorn Sep 16 '13 at 03:44
  • It usually means you tried to compile with `gcc` instead of `g++` (the latter automatically links the C++ standard library). – Jerry Coffin Sep 16 '13 at 03:44
  • 2
    `friend istream& operator>>(istream&, const Complex &C);` doesn't match the implementation. You probably want to remove the extra const. – Retired Ninja Sep 16 '13 at 03:45
  • Derp, I completely forgot I removed the const when writing the function. My god moving back to C++ from C# is confusing :S – clifford.duke Sep 16 '13 at 03:46
  • Live and die by cut/paste. Sometimes it'll bite you. And you were *supposed* to remove the const for that function. An extractor with a const rhs is somewhat pointless. The friend proto is the problem. Remove that one as well. – WhozCraig Sep 16 '13 at 04:00
  • Yeah, I was being lazy and copied it over, then realized I was an idiot for making it a const, like you said, whats the point lol. Why do I need to remove friend? – clifford.duke Sep 16 '13 at 04:12
  • This program compiles in g++ 4.4, except that you must include for `abs` function. – cpp Sep 16 '13 at 04:29

0 Answers0