Error in Operator Overloading

Hello,
I am currently trying to do a problem out of the back of a C++ book I have that is asking me to do several things, one of which is overload several operators to work with a user defined class. Codeblocks is giving me a compile error that I'm having problems finding anything out about.
"'Bool input::operator<(const input&, const input&)' must take exactly one argument."
I found out through some searching that I should not necessarily need to send both input classes to the operator, but if I take one argument out, It gives me the same error but it's asking for exactly two arguments instead. I commented the three lines in the header file.

input.h:
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
 //class header
#ifndef INPUT_H
#define INPUT_H
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class input{
public:
bool decide;


input(double, double);
void printOut();
void convertDiv(double, double, double, double);
double getTop() const;
double getBottom() const;
double getRealNumb() const;
void lessThan(const input & t1, const input & t2);
void display(ostream & out);
bool operator < (const input &t1, const input &t2);// error: must take exactly one argument.
bool operator > (const input &t1, const input &t2);//error: must take exactly one argument
void invert();

//private:
double num1;
double num2;
double num3;
double num4;
double realnumb;
double hold1;
double hold2;
double hold3;

void operator<<( ostream & out, const input & t);//error: must take exactly one argument

};

#endif


input.cpp:
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
54
55
56
57
58
#ifndef INPUT_CPP
#define INPUT_CPP
#include <iostream>
#include <string>
#include "input.h"
using namespace std;

input::input( double a, double b){
num1 = a;
num2 = b;
realnumb = a /  b;
cout << num1 << " / " << num2 << endl;
cout << "REal number: " << realnumb << endl;
}
double input::getTop() const{
    return num1;

}
double input::getBottom() const{
    return num2;

}
double input::getRealNumb() const{
    return realnumb;

}
void input::invert(){
    cout << "BEfore invert " << num1 << " / " << num2 << endl;
    hold1 = num1;
    num1 = num2;
    num2 = hold1;
    cout << num1 << " / " << num2 << endl;
}
void input::lessThan(const input & t1, const input & t2) {
    decide = (t1.getRealNumb() < t2.getRealNumb());
    cout << decide << endl;
}
void input::display(ostream & out)
{
    cout << realnumb << endl;
   // cout<< a << " / " << b << endl;

}
 ostream & operator<<(ostream & out, input & t)
{
    t.display(out);
    return out;
}
 bool operator<(const input & t1, const input & t2)
{
    return t1.getRealNumb() < t2.getRealNumb();
}
 bool operator>(const input & t1, const input & t2)
{
    return t1.getRealNumb() > t2.getRealNumb();
}

#endif // INPUT 


main.cpp:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;
#include "input.h"


int main(void)
{   string hold;
	double x;
	char switcharino;
	double num1;
	double num2;
	double hold1;
	double hold2;
	double test1;
	double test2;
	cout << "Please enter your fraction. It will be x/y"<< endl;
	cin >> num1;
	cout << num1 << " / x" << endl;
	cout << "enter the number for X. " << endl;
	cin >> num2;
	input firstFrac(num1, num2);
	cout << "Please enter your  Second fraction. It will be x/y"<< endl;
	cin >> num1;
	cout << num1 << " / x" << endl;
	cout << "enter the number for X. " << endl;
	cin >> num2;
	input secondFrac(num1, num2);

	cout << "Please enter what operation you would like. " << endl;
	cout << "___________________________________________________________" << endl;
	cout << "+  : add two together " << endl;//not done
	cout << "-  : subtract two " << endl;//not done
	cout << "/  : divide two " << endl;//not done
	cout << "*  : multiply two " << endl;//not done
	cout << "I  : Invert the fraction " << endl;//not done
	cout << "M  : Convert to mixed fraction" << endl;//not done
	cout << "R  : Reduce to lowest term" << endl;//not done
	cout << "G  : Obtain greatest divisor of numerator and denominator" << endl;//not done
	cout << "L  : Lowest common denominator for a/b and c/d " << endl;//not done
	cout << "1  : test a/b <  c/d" << endl;//not done
	cout << "2 : test a/b <= c/d" << endl;//not done
	cout << "3  : test a/b >  c/d" << endl;//not done
	cout << "4 : test a/b >= c/d" << endl;//not done
	cout << "5  : test a/b =  c/d" << endl;//not done
	cout << "X  : solution of linear equation (a/b)X + c/d = e/f" << endl;//not done
	cout << "___________________________________________________________" << endl;
	cin >> switcharino;

    switch(switcharino){
        case '+':/* + */;
            cout << "+ switch" << endl;
                break;
        case '-':/* - */;
            cout << "- switch" << endl;
            break;
        case '/':/* / */;
            cout << "/ switch" << endl;
            break;
        case '*':/* * */;
            cout << "* switch" << endl;
            break;
        case 'I':/* Invert */;
            cout << "Inverter switch" << endl;
            firstFrac.invert();
            break;
        case 'M':/* Convert mixed */;
            cout << "Mixed converter switch" << endl;
            break;
        case 'R':/* reduce to lowest */;
            cout << "reduce lowest switch" << endl;
            break;
        case 'G':/* greatest divisor */;
            cout << "greatest switch" << endl;
            break;
        case 'L':/* lowest common */;
            cout << "Mixed converter switch" << endl;
            break;
        case '1':/* < */;
            cout << "< switch" << endl;
            cout << (firstFrac < secondFrac) << endl;
            break;
        case '2':/* <= */;
            cout << "<= switch" << endl;
            break;
        case '3':/* > */;
            cout << "> switch" << endl;
            break;
        case '4':/* >= */;
            cout << ">= switch" << endl;
            break;
        case '5':/* == */;
            cout << "== switch" << endl;
            break;
        case 'X':/* linear bullshit */;
            cout << "linear switch" << endl;
            break;
        case 'T':
            //cout << areEqual() << endl;

        default:
            cout << "not correct " << endl;
            break;
    }
}

I'm inexperienced with classes so I thought that maybe it was a permission issue, so I made everything public. This however didn't fix any of the errors.
I appreciate any help you guys can give me.
In the header file you declare the operators as members of the class. All functions which are members of a class receive one argument implicitly. That is the this pointer which is the first argument received by the function. So, the correct way to declare an operator in the class is:

1
2
3
4
class input {

    bool operator<(const input& other) const ;   // this receives two arguments.  this and other.
};


In your source file, despite declaring the operators as members of the class, you define them as free-standing functions.

Instead of:
1
2
3
4
 bool operator<(const input & t1, const input & t2)
{
    return t1.getRealNumb() < t2.getRealNumb();
}


You should have:
1
2
3
4
5
6
bool input::operator<(const input& other) const 
{
    return getRealNumb() < other.getRealNum();
    // or if you prefer:
    // return this->getRealNum() < other.getRealNum();
}



Also the operator <<
which was declared like this:
void operator<<( ostream & out, const input & t);
should return a reference to the ostream. To allow it access to private member variables, make it a friend:
 
friend std::ostream & operator<<( std::ostream & out, const input & t);


Then the function might be defined something like this:
1
2
3
4
5
std::ostream & operator<<( std::ostream & out, const input & t)
{
    out << t.num1 << '/' << t.num2;
    return out;
}

Those two tips solved the error, thank you guys!
Topic archived. No new replies allowed.