i keep getting this error: error: expected ‘)’ before ‘,’ token this is the code i'm using:

dragon(string str = "wild dragon", int level = 5);

this is inside a the dragon class definition.

dragon::dragon(str,level) {
    name = str;
    tp = ((rand() % 6) + 1);
    type = tp;
    lvl = level;
    hp = hpmax;
    setout(false);/* set to conscious */

this is where the class is instantiated:

pets[i] = new dragon(name,level);

the variables name and level where assigned earlier in the loop.

could someone give me a hint where i'm going wrong?
this is a text based rpg i'm doing for fun.

Edited by edrag: n/a

6 Years
Discussion Span
Last Post by writerervin

The problem might be that ye' are breaking one of the rules on the use of default arguments. Here is a excerpt from MSDN:

Default arguments are used only in function calls where trailing arguments are omitted — they must be the last argument(s). Therefore, the following code is illegal:
Copyint print( double dvalue = 0.0, int prec );

A default argument cannot be redefined in later declarations even if the redefinition is identical to the original. Therefore, the following code produces an error:
Copy// Prototype for print function.
int print( double dvalue, int prec = 2 );


// Definition for print function.
int print( double dvalue, int prec = 2 )
The problem with this code is that the function declaration in the definition redefines the default argument for prec.

Edited by Clinton Portis: n/a


I fixed the function prototype like you suggested.

dragon(string str, int level = 5);

and fixed the code where it's called in main

wild = new dragon("wild dragon");

but i'm getting the same error. what else can i do?


On line 1 of your second code listing of the first post, you still need the datatypes for str and level in the constructor definition

dragon::dragon(string str,int level) {
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.