Default Arguments in functions

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) {
   ...
}

Default parameters must be specified in the declaration (and only there).
you only defined default arguments in the declaration and not the definition.
Ha, typical, I tried every combination but that one. Thanks guys.
Topic archived. No new replies allowed.