0

I am trying to declare this function in a .cpp file, but get compilation error:

error: default argument given for parameter 2 of 'void print_vector(std::vector&, char*)'

void print_vector(vector<int>&c, char *title="");

How can I declare it correctly?

0

2 Answers 2

1

The declaration you are showing is not sufficient to produce this error, but it can arise if you separate the function declaration and implementation and accidentally have the default in both. Be sure to only specify the default parameter in one of them.

I.e. instead of

// declaration, usually in .h
void print_vector(vector<int>&c, const char *title="");

// implementation, usually in .cpp
void print_vector(vector<int>&c, const char *title="") {
    // code
}

do

// declaration, usually in .h
void print_vector(vector<int>&c, const char *title="");

// implementation, usually in .cpp
void print_vector(vector<int>&c, const char *title) {
    // code
}

or the other way round, i.e. with the default value in the implementation (though the way shown above is usually preferred, because it makes the behavior and usage clear to a reader of your header file). Also notice the const char* to avoid warnings (or even errors as pointed out by AndreyT, thanks!).

Sign up to request clarification or add additional context in comments.

8 Comments

Strictly speaking, conversion of string literal to char * is no longer a mere warning. It was removed from the language entirely in C++11.
@Oguk you are right. My implementation is the same as the declare like the 1st part in your answer. Once I change it to 2nd park, it works.
string title="" has the same issue. In this case, string or char * is not the key point.
@user1443721 Of course that's not the key point. Who was talking about "string"!? This rule holds for default parameters of any type. AndreyT was talking about the "const" in "const char*" which is now strictly necessary. A string literal is of type "const char*". The type "std::string" is a different beast.
@Oguk "Or the other way round" is not good. Think about one function call like print_vector(c);
|
0

A declaration of a function only says how this function may be called. It need not to match, and in some cases it may not match. Consider a case:

class T
{
   static int GetDefault();
};

int T::GetDefault()
{
   return 0;
}

Here, it not not required to use static keyword in implementation. Some compiler may render a warning, or some static analyses tool me render warning (although it doesn't harm). Also, note that parameter name may not match (or may be omitted in either declaration or implementation), since only base prototype is needed for the callers.

int Compute(int,int);
int Compute(int a, int b) 
{
    return a*b;
}

In case of default argument, you give default argument for the callers, and not for the implementation of function itself (unless implemented inline in class).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.