Reputation: 23
I'm getting a warning in my constructor in my class, and I've never seen it before. This is what my constructor looks like.
Account(std::string n = "NULL", std::string i = "0", Stats s = (0,0,1) )
: name(n), id(i), stat(s) {}
If I remove any of these commas it results in a compile error, no? Is this warning incorrect, or is there something I can change to fix it?
Upvotes: 1
Views: 30658
Reputation: 83235
The issue is this: (0,0,1)
.
That is parentheses around the expression 0,0,1
, which evaluates to 1
. (The comma operator is an infix operator that evaluates the first and second expression and returns the second. In this case, you have two such operators.)
I don't know what you wanted there, but I'm guessing that isn't it.
EDIT: It seems you want Stats s(0,0,1)
.
Upvotes: 4
Reputation: 10667
The problem is most likely in that part Stats s = (0,0,1)
. C++ sees this as a sequence of expression to be computed while keeping only the last value. It returns 1. You probably mean
Stats s = Stats(0,0,1)
which works if your stats structure have such a constructor. Note that in C++11 you have this shorter syntax:
Stats s = {0,0,1}
Upvotes: 3
Reputation: 595
I think what you are looking for is to give a default value to function argument which is a struct, and I think it was discussed here How can I assign a default value to a structure in a C++ function?
Upvotes: 0