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?
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 statements.printOut();but correctly not finding it. What you need is:And in your cpp :
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.