Problem with multyple function with same name but different parameters and i get errors

Started by
6 comments, last by Lactose 10 years ago

Hello.

AFAIK this should work:

If you know why i am getting this, please help!


//HPP
void AddSpecialEffect(int posX, int posY, SpecialEffect & specialEffect, int * SpecialEffectVecID);
void AddSpecialEffect(int UnitVecID, SpecialEffect & specialEffect, int * SpecialEffectVecID);
 
//CPP
void SpecialEffectHolder::AddSpecialEffect(int PosX, int PosY, SpecialEffect & specialEffect, int * SpecialEffectVecID = 0)
{
    
}
void SpecialEffectHolder::AddSpecialEffect(int UnitVecID, SpecialEffect & specialEffect, int * SpecialEffectVecID = 0)
{
   
}

The compiler error


1>main.cpp(109): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(110): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(111): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(112): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(114): error C2661: 'SpecialEffectHolder::AddSpecialEffect' : no overloaded function takes 2 arguments
Advertisement

What's the calling code in main, line 109?

Hello to all my stalkers.

Write the default value into the declaration, not into the definition:


//HPP
void AddSpecialEffect(int posX, int posY, SpecialEffect & specialEffect, int * SpecialEffectVecID = 0);
void AddSpecialEffect(int UnitVecID, SpecialEffect & specialEffect, int * SpecialEffectVecID = 0);


//CPP
void SpecialEffectHolder::AddSpecialEffect(int PosX, int PosY, SpecialEffect & specialEffect, int * SpecialEffectVecID)
{
    
}
void SpecialEffectHolder::AddSpecialEffect(int UnitVecID, SpecialEffect & specialEffect, int * SpecialEffectVecID)
{
   
}


Write the default value into the declaration, not into the definition:

I'm curious as to why this would be a fix. I thought putting the default in either the declaration or the definition was allowed (but not both) -- although I do put my defaults in the declaration as well, as I find it more user friendly.

Stack Overflow seems to also agree with both being valid: http://stackoverflow.com/questions/4989483/where-to-put-default-parameter-value-in-c

Hello to all my stalkers.

Both are valid, it's just that putting them into the implementation is in almost all cases useless (and equivalent with not having them at all). The compiler needs to see the default parameter to use it (which it can only do if they are in the header when it is working on a different compilation unit).

The error probably happens because the OP is calling the function with three parameters and the only overload that makes sense in that case is the (int, SpecialEffect&, int*)-version, which obviously does not work out.
I'm curious as to why this would be a fix.

At least the last error

1>main.cpp(114): error C2661: 'SpecialEffectHolder::AddSpecialEffect' : no overloaded function takes 2 arguments

let me assume that the main.cpp includes the HPP, finds a 3 and a 4 parameter variant of the routine, but has an invocation with 2 arguments. Hence it is not aware that the 3 parameter variant has a default argument and hence should be invokable with 2 arguments, too.

Now assuming that the other errors

1>main.cpp(109): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(110): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(111): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'
1>main.cpp(112): error C2664: 'void SpecialEffectHolder::AddSpecialEffect(int,SpecialEffect &,int *)' : cannot convert parameter 2 from 'int' to 'SpecialEffect &'

are invoked with 3 arguments although targeting the 4 parameter variant, the compiler tries to fit it into the one and only 3 parameter variant it knows of, and requires the int to reference conversion.

What's the calling code in main, line 109?

Do note i may switch these up and down was testing, apparently adding adding last parameter "NULL" makes the thing work!


objSpecialEffectHolder.AddSpecialEffect(0, SpecialEffect(en::SEL2, 5, 4, 100, sf::Color(255,255,255,255)), NULL);

This is the main call



objSpecialEffectHolder.AddSpecialEffect(0, SpecialEffect(en::SEL2, 5, 4, 100, sf::Color(255,255,255,255)));
    objSpecialEffectHolder.AddSpecialEffect(10, 10, SpecialEffect(en::SEL1, 1, 3, 500, sf::Color(255,255,255,255)));
    objSpecialEffectHolder.AddSpecialEffect(100, 100, SpecialEffect(en::SEL1, 1, 3, 422, sf::Color(255,255,255,255)));
    objSpecialEffectHolder.AddSpecialEffect(99, 290, SpecialEffect(en::SEL1, 0, 4, 500, sf::Color(255,255,255,255)));
    objSpecialEffectHolder.AddSpecialEffect(21, 57, SpecialEffect(en::SEL1, 0, 4, 266, sf::Color(255,255,255,255)));

Write the default value into the declaration, not into the definition:


//HPP
void AddSpecialEffect(int posX, int posY, SpecialEffect & specialEffect, int * SpecialEffectVecID = 0);
void AddSpecialEffect(int UnitVecID, SpecialEffect & specialEffect, int * SpecialEffectVecID = 0);


//CPP
void SpecialEffectHolder::AddSpecialEffect(int PosX, int PosY, SpecialEffect & specialEffect, int * SpecialEffectVecID)
{
    
}
void SpecialEffectHolder::AddSpecialEffect(int UnitVecID, SpecialEffect & specialEffect, int * SpecialEffectVecID)
{
   
}

This also fixes it, sad.png so much of my code has potential to break, default parameter in declaration.

thanks on information all!

I see, that makes sense.

Thanks for clarifying.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement