0

This is my pt2 of my previously posted question which idk if its gonna be answered or not after I edited since it was already considered 'answered' i think.

Ok so I'm now trying for output a + bi :

std::ostream& operator<< (std::ostream& out, complex const& c) {
    return out << c.getReal() << "+" << c.getImag() << "i";
}

and for input:

std::istream& operator>> (std::istream& in, complex& c) {
    double h, j;
    if (in >> h >> "+" >> j >> "i") {
        c.set(h, j);
    }
    return in;
}

However I get the following error when I compile:

This is for line 181 of my complex.cpp file(class complex implementation file) where if (in >> h >> "+" >> j >> "i") {is located of the above function definition is located:

binary '>>': no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion) 

the following are all for line 45(Note each error is separate,total of 7 for this line) of my complex.h file where friend std::istream &operator>> (std::istream &in, complex& c); protoype is located.

'istream':is not a member of 'std'

syntax error missing ';' before '&'

'istream':'friend' not permitted on data declarations

missing type specifier-int assumed. Note:C++ does not support default-int

unexpected token(s) preceding';'


namespace "std" has no member "istream"

namespace "std" has no member "istream"

the following are for line 46 of my complex.h file where

friend std::ostream &operator<<(std::ostream &out, complex c);

is located

'ostream': is not a member of 'std'

syntax error: missing ';' before '&'

'ostream':'friend' not permitted on data declarations

missing type specifier -int assumed.Note: C++ does not support default-int

unexpected token(s) preceding ';'

namespace "std" has no member "ostream"

namespace "std" has no member "ostream"

I noticed both are the same type of errors. Note I have

#include<iostream>

using namespace std;

both on the complex.cpp file and the main.cpp file

GustX
  • 45
  • 1
  • 10

1 Answers1

3

You are trying to input into read only string literals in

if (in >> h >> "+" >> j >> "i")

Which is not going to work. What you need to do is create a variable to store the text content of the input. Since the content is not needed we can just toss it out when done. That will give you something along the lines of

std::istream& operator>> (std::istream& in, complex& c) {
    double h, j;
    char eater;
    if (in >> h >> eater >> j >> eater) { // eater now consumes the + and i
        c.set(h, j);
    }
    return in;
}

As for the errors in you header files you need to have #include <iostream> in your header file so the compiler knows what istream and ostream are.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Hmn that makes sense, But my ostream doesn't need fixes? I try to compiler it with the istream fix but still get 14 out of 15 of the same errors above ( just the binary '>>' error is gone now.) – GustX Dec 12 '15 at 00:24
  • @GustX DId you add `#include ` to the begining of complex.h? – NathanOliver Dec 12 '15 at 00:27
  • Oh I wasn't sure which headers i'm supposed to add to where. Finally it compiles without errors, but now I'm trying to test my overloads using complex class objects I get 1 error called "unresolved externals", Should I edit this question or post a new one?(before I compiled without having written anything in the main.cpp file) – GustX Dec 12 '15 at 01:25
  • @GustX That should be a new question although you should see this first: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – NathanOliver Dec 12 '15 at 01:31
  • damn, thats alot i will have to investigate later not much time left for this D: – GustX Dec 12 '15 at 01:51