Operator overloading , comparing values-HELP

Trying to compare two values using operator overloading , quite sure i am messing up in the output part but dont know how to fix it , except using booleans , havent studied that yet.Help ?

Error: Illegal structure operation in main() in line 35&36

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
# include <iostream.h>
class opp
{
private:
int number;
public:
void get();

opp operator <(opp);

};
void opp::get()
{
int num;
cin>>num;
number=num;
}

opp opp::operator <(opp n)
{
opp temp;
if (temp.number < n.number)
return temp;
else
return n;
}

int main()
{
opp n1;
opp n2;
n1.get();
n2.get();

if (n1<n2)
cout<<n1<<"smaller";
else
cout<<"other shit";
return 0;
}
> Error: Illegal structure operation
quite an awful error message. Compare against gcc and clang
foo.cpp:35:8: error: could not convert ‘n1.opp::operator<(n2)’ from ‘opp’ to ‘bool’
foo.cpp:36:7: error: no match for ‘operator<<’ in ‘std::cout << n1’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘opp’)

foo.cpp:35:6: error: value of type 'opp' is not contextually convertible to 'bool'
foo.cpp:36:7: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'opp')
`n1<n2' should not be `min(n1,n2)'
and you never say how an `opp' object will be printed.


> Trying to compare two values
1
2
3
4
opp opp::operator <(opp n)
{
   opp temp;
   if (temp.number < n.number)
¿what does `temp' have to do with anything?


PS: your compiler is ancient, the header is just `iostream' without the .h
Actually my compiler is shit when it comes to <iostream> without the H

and if i am comparing 2 objects , there will be 2 objects right ?

temp and N ?

and i should use min instead of n1<n2? why isnt it working this way ?

and wat was that thing about never saying how it will be printed ?

Sorry for too many questions :P
Last edited on
What does comparison operator return in this example?
3<2;

And what do you really want ot return here?
1
2
3
4
opp n1;
opp n2;

n1<n2


Is it class object n1 or n2 or is it in fact true / false?

Because if you are using if (n1<n2) it seems you meant the last one. From what I know if can accept only argument true or false.

So your operator should be more like bool operator< ...
ya thats what i was confused about how to output it, so it has to be done by boolean.The fucking book never explained boolean operators and gave this question.

Anyway
Thnx a lot.
Whats wrong with this now ?

Annowing errors :/

Type name expected (line 9)
Declaration missing ; (line 9)
Illegal structure operation in function main (line 35)


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
# include <iostream.h>

class opp
{
private:
int number;
public:
void get();
bool operator <(const opp& d)
{
if(number<d.number)
{
return true;
}
return false;
}

};
void opp::get()
{
int num;
cin>>num;
number=num;
}



int main()
{
opp n1;
opp n2;
n1.get();
n2.get();

if (n1<n2)
cout<<"smaller";
else
cout<<"other shit";
return 0;
}
I get no errors compling this in VS2010.

From the #include <iostream.h>, I gather your compiler is ancient.
phew! thats a relief
Topic archived. No new replies allowed.