overloading the '>>' operator cause error.

Hi there everyone.

Nearly finished going through the "C++ A beginners guide" by Herbert Schildt.

In Chapter 11 it talks about overloading the << and >> operators to allow classes to work with cin and cout.

Now following his example I got the overloaded '<<' operator to work but not the '>>' operator. I've checked my syntax again and again... it seems fine but keep getting the error message
c:\...\main.cpp|51|error: no match for 'operator>>' in 'std::cin >> testedclass'|
.

Here's my code in it's entirety - like I said the '<<' operator works fine...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

class tester
{
    int x, y, z;

public:

    friend ostream &operator<<(ostream &stream, tester c2o);
    friend istream &operator>>(istream &stream, tester testerinput);

    tester(int a, int b, int c)
    {
        x = a;
        y = b;
        z = c;
    }

    tester()
    {
        x = y = z = 0;
    }

    int get_x();
};

ostream &operator<<(ostream &stream, tester c2o)
{
    stream << c2o.x << " ";
    stream << c2o.y << " ";
    stream << c2o.z << endl;
    return stream;
}

istream &operator>>(istream &stream, tester testerinput)
{
    return stream;         //stripped this back to diagnose prob...
}                          //will have more code when working.

int main()
{

    tester testedclass();

    cin >> testedclass;                //This is the line that causes the error


    cout << testedclass << endl;

    cout << "Hello world!" << endl;
    return 0;
}


Any ideas? Could it be something to do with the compiler itself? Or maybe it's libraries?

Or am I just missing something...

More than likely the latter, lol.
Line 44 is declaring a function.
Also, you should pass testerinput by reference in the operator
Yup see that now!!

Bascially I original had some arguements within the parenthese on line 44 (setup variables). I removed them to test the default constructer... forgot to remove parenthese... works a charm now - many thanks Bazzy :)
Topic archived. No new replies allowed.