Default Arguments in functions
Nov 1, 2010 at 5:34pm UTC
Hi All,
Having come from PHP I have a few habits that are hard to kick. One such habit is being able to declare a default argument in a function
1 2 3 4 5 6 7
function foo($arg, $arg2 = "bar" ) {
...
}
foo("bar" , "car" );
foo("bar" );
Is the same sort of thing possible in C++? I have tried the same syntax as PHP, and this did not give me any syntax errors in the G++ compiler, I did get an error
./log.h:5: error: default argument given for parameter 3 of ‘int logEvent(int, std::string, bool)
Here is the code im using:
1 2 3 4 5 6 7 8 9 10
#include <fstream>
#include <time.h>
#include <string.h>
int logEvent(int log, std::string logEvent, bool echo = true );
int flush_logFile(int log, std::string logEvent);
int logEvent(int log, string logEvent, bool echo = true ) {
...
}
Nov 1, 2010 at 5:40pm UTC
Default parameters must be specified in the declaration (and only there).
Nov 1, 2010 at 5:46pm UTC
you only defined default arguments in the declaration and not the definition.
Nov 1, 2010 at 6:07pm UTC
Ha, typical, I tried every combination but that one. Thanks guys.
Topic archived. No new replies allowed.