Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default parameter acting weird

Status
Not open for further replies.

dds82

Programmer
Jun 5, 2002
251
US
I have a function that I declared like this in the header file:

Code:
CString func(const SomeClass &x, bool y=true);

and the compiler gives me error C2572 ("Redefinition of default parameter") on this line in the header file, not the C++ source file. However, when I change the line to

Code:
CString func(const SomeClass &x, bool y);

I get errors in other files that say "Error: 'func' does not take 1 parameter(s)".

What's going on?
 
CString func(const SomeClass &x, bool y=true);
You can use it if Your Function is a class member. Questions:
1.Maybe, You have another Function with such name in this class?
2. Is the class SomeClass defined before this line and known for the header (for example, so:
class SomeClass;)
3. Maybe, You have class member named y? For example, so:
class SomeClass;
class MySuperClass
{
int x;
int y;
CString func(const SomeClass &x, bool y=true);
...
};
Try to change them.
If it all does not work, easy add to Your class (for example, after int y;) such function:
inline CString func(const SomeClass &x)
{
return func(x, true);
}
 
This is a global function. It's not part of any class.

Right now, I've already done your last suggestion; it's the only thing I could get to work. But why is it giving trouble with the default parameter?
 
The problem is only if You write:
int func(bool y = true);

int func(bool y = true)
{
return 2 * y;
}

Use that instead:
int func(bool y = true);

int func(bool y ) //No default value in definition
{
return 2 * y;
}
 
Yes. I knew that. That's not the problem here. I didn't do that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top