Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Default Argument Error

Any Idea why this error is coming up at compile time?

ComplexNumber.cpp:21: error: default argument given for parameter 1 of ‘void ComplexNumber::print(std::ostream&) const’
ComplexNumber.h:17: error: after previous specification in ‘void ComplexNumber::print(std::ostream&) const’

Here is my code at those certain areas:

ComplexNumber.cpp

21    void ComplexNumber::print(ostream & out = cout) const {

ComplexNumber.h

17    void print(ostream & out = cout) const;
like image 596
Pat Murray Avatar asked Apr 25 '12 20:04

Pat Murray


People also ask

Is there default argument in C?

Generally no, but in gcc You may make the last parameter of funcA() optional with a macro.

Which is the correct condition for the default arguments?

Which is the correct condition for the default arguments? Explanation: The default arguments must be declared at last in the argument list. This is to ensure that the arguments doesn't create ambiguity.

What do you mean by default arguments?

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language).

Can we pass default arguments to overloaded functions?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.


1 Answers

You should only specify the default parameter in the function declaration, i.e. in the header. You implementation should look something like this:

void ComplexNumber::print(ostream & out) const { ..... }
like image 149
juanchopanza Avatar answered Oct 31 '22 21:10

juanchopanza