Reputation: 1933
I have a problem with specifying the default values for my C++ class members. My code is:
From Someclass.h:
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut (bool);
}
...from SomeClass.cpp:
void SomeClass::printOut(bool foobar=true)
{
if (foobar) { std::cout << foobar << std::endl; }
}
...and finally, from main.cpp:
int main()
{
SomeClass s;
s.printOut();
return 0;
}
This however gives error message (gcc):
../main.cpp: In function `int main()':
../main.cpp:8: error: no matching function for call to `SomeClass::printOut()'
../SomeClass.h:18: note: candidates are: void SomeClass::printOut(bool)
subdir.mk:21: recipe for target `main.o' failed
make: *** [main.o] Error 1
I have tried specifying the default value directly into the class declaration in the header file, etc. I also tried searching both Stack Overflow and Google in general, but cannot find any solution anywhere. What am I doing wrong?
Upvotes: 8
Views: 11286
Reputation: 40947
You haven't specified the default value for the parameter in the header as such, the compiler is looking for a function of signature void printOut(void)
for your statement s.printOut();
but correctly not finding it. What you need is:
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut( bool fValue = true ); // Note change in param in definition
}
And in your cpp :
void SomeClass::printOut(bool foobar /*=true*/ )
{
if (foobar) { std::cout << foobar << std::endl; }
}
As a side note, bear in mind you don't have to put the commented out default value for the parameter in the implementation file but it is a good idea for readability.
Upvotes: 10
Reputation: 11787
the default value has to be specified in the declaration and not in the definition. You can specify default value at both the places but cant omit from definition. hope i dint confuse you. I will show the e\corrected code so you can understand:
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut (bool foobar = true);
}
...from SomeClass.cpp:
void SomeClass::printOut() //or you can use: void SomeClass::printOut(bool foobar=true)
{
if (foobar) { std::cout << foobar << std::endl; }
}
...and finally, from main.cpp:
int main()
{
SomeClass s();
s.printOut();
return 0;
}
Upvotes: 0
Reputation: 10126
Try to specify default value in header file:
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut (bool foobar=true);
}
Upvotes: 1
Reputation: 6484
rewrite as follow .. note bool b=false
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut (bool b=false);
}
Upvotes: 1
Reputation: 3554
Default parameters must be defined in the header like this:
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut (bool value = true);
}
Upvotes: 1
Reputation: 258618
You need to declare the default value inside the class definition, not in the implementation.
class SomeClass
{
public:
SomeClass();
~SomeClass();
void printOut (bool foobar = true); //move default here
}
void SomeClass::printOut(bool foobar) //remove from here
{
if (foobar) { std::cout << foobar << std::endl; }
}
Also, note that:
SomeClass s();
doesn't do what you expect it to do. It doesn't create an object s
of type SomeClass
, but declares a function s
with return type SomeClass
. s.printOut();
shouldn't compile.
You probably want:
SomeClass s;
Upvotes: 5
Reputation: 24546
Default value must be specified in the declaration of the method, not in the implementation.
Upvotes: 1