Guzfraba wrote: |
---|
"The class has just one operator+ that takes two objects." |
The "
test" class
does not have a + operator; it allows a certain globally-overloaded + operator to access its data-members. In your code, a globally-overloaded + operator has been defined, but the arguments you've passed it do not correspond to the following declaration (so the compiler will attempt to call a suiting constructor of "
test". See blow):
|
friend test operator+(const test&,const test&);
|
What you need to do is add another (or modify the original) declaration that supports the necessary operands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// Inside "test" class:
friend test operator + ( test const &, int const );
// Global name-space:
test operator + ( test const &Left_, int const Right_ )
{
return( ( Left_.num + Right_.num ), ( Left_.den + Right_.den ) );
}
int main( )
{
test ObjectA_( 5 );
ObjectA_ = ( ObjectA_ + 5 ); // Calls ::operator + ( test &, int );
}
|
Granted, the compiler will implicitly convert the 1 on line 37 to a "
test" instance, but it's best to support such trivial types, such as "
int", in special cases. If this is not the behaviour you want, then declare the following constructor as "
explicit":
|
explicit test::test(int nnum);
|
Guzfraba wrote: |
---|
"Is the int converted to a test object? How?" |
When the compiler calls "
::operator + ( test &, test & )" with 1 as the right-hand operand, the compiler will take the 1 and call the a constructor of "
test" which accepts an "
int" as a parameter.
Wazzak