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
# 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;
elsereturn 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
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.