Why doesn't my default argument work?
Sep 8, 2011 at 3:45am UTC
I'm experimenting with default arguments but they don't deem to be working for me. On line 43 I make a call to a function that's declared on line 9 and defined on line 105. When I run this program I get an error on line 43:
'rational::output' : function does not take 0 argument
and on line 106:
'void rational::output(std::ostream &)' : overloaded member function not found in 'rational'
What am I doing wrongly?
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#include <iostream>
using namespace std;
class rational
{
public :
rational();
void output(ostream& out) const ;
void set(short num, short den);
short get_numerator() const ;
short get_denominator() const ;
rational add(rational number);
rational sub(rational number);
rational sub(short number);
rational mul(rational number);
rational mul(short number);
rational div(rational number);
rational div(short number);
rational neg();
private :
short numerator;
short denominator;
};
int main()
{
char loop = 'y' ;
do
{
rational rational1, rational2;
cout << "Input the numerator and denominator: " ;
short num, den;
cin >> num >> den;
rational1.set(num, den);
cout << "Input the numerator and denominator: " ;
cin >> num >> den;
rational2.set(num, den);
//testing
rational1.output();
cout << "What would you like to do?\n"
<< "1) add\n"
<< "2) subtract\n"
<< "3) multiply\n\n" ;
char choice;
cin >> choice;
while (choice != '1' && choice != '2' && choice != '3' )
{
cout << "Invalid choice. What would you like to do?\n"
<< "1) add\n"
<< "2) subtract\n"
<< "3) multiply\n\n" ;
cin >> choice;
}
rational result;
if (choice == '1' )
{
result = rational1.add(rational2);
cout << "The result is: " ;
result.output(cout);
}/*
else if(choice == '2')
{
result = rational1.sub(rational2);
cout << "The result is: ";
result.output(cout);
}
else if(choice == '3')
{
result = rational1.mul(rational2);
cout << "The result is: ";
result.output(cout);
}*/
cout << endl;
cout << "Would you like to run the program again? (y/n)? " ;
cin >> loop;
loop = tolower(loop);
while (loop != 'y' && loop != 'n' )
{
cout << "Incorrect output. Would you like to run the program again? " ;
cin >> loop;
}
}while (loop == 'y' );
cout << "Press a key to exit: " ;
char exit;
cin >> exit;
return 0;
}
rational::rational() : numerator(0), denominator(1)
{
//default constructor
}
void rational::output(ostream& out = cout)
{
out << numerator << "/" << denominator;
}
void rational::set( short num, short den = 1)
{
numerator = num;
denominator = den;
}
short rational::get_denominator() const
{
return denominator;
}
short rational::get_numerator() const
{
return numerator;
}
rational rational::add(rational number)
{
rational sum;
sum.numerator = numerator*number.denominator+number.numerator*denominator;
sum.denominator = denominator*number.denominator;
return sum;
}
/*
rational rational::sub(rational number)
{
numerator = numerator*number.denominator - denominator*number.numerator;
denominator = denominator*number.denominator;
}
rational rational::sub(short integer)
{
numerator = numerator - integer*denominator;
}
rational rational::mul(rational number)
{
numerator = numerator * number.numerator;
denominator = denominator * number.denominator;
}
rational rational::mul(short integer)
{
numerator = integer * numerator;
}*/
Sep 8, 2011 at 4:06am UTC
Put the default expression (the "= cout" part) in the declaration, not the definition. Otherwise, the compiler can't know that out is a default parameter.
Sep 9, 2011 at 6:13am UTC
That helped. Thanks.
Topic archived. No new replies allowed.