Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ ...when all the arguments have default values

I guess that this is a very absurd/basic question, but still:

class m
{
public:
   void f(int ***);
  /***/
}
void m::f(int ***a = NULL)
{
  /***/
}

The call to f (as well as any function which has default values for all the arguments) doesn't accept 0 arguments. Why? How should I format the declaration then?

like image 982
huff Avatar asked Dec 03 '22 13:12

huff


2 Answers

That works fine if the function definition is in the header file. The rule is that whoever is calling the function has to 'see' the default value.

So, I'm guessing you have the function definition in a separate source file. Assuming that's the case, just put the default in the function declaration (in the class):

class m
{
public:
   void f(int *** = 0);
  /***/
};

You'll also need to remove the default value from the function definition as you can only define the default in a single place (even if the value itself is the same).

like image 108
R Samuel Klatchko Avatar answered Dec 25 '22 19:12

R Samuel Klatchko


This will work:

class m
{
public:
   void f(int ***a = NULL);
};

void m::f(int ***a)
{
}
like image 21
Eli Bendersky Avatar answered Dec 25 '22 19:12

Eli Bendersky