Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1232

Like
0Likes
Like

Posted 04 April 2014 - 05:34 AM

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

Edited by BaneTrapper, 04 April 2014 - 05:36 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Lactose!   GDNet+   -  Reputation: 3626

Like
0Likes
Like

Posted 04 April 2014 - 05:41 AM

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



#3 haegarr   Crossbones+   -  Reputation: 4438

Like
8Likes
Like

Posted 04 April 2014 - 05:41 AM

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


#4 Lactose!   GDNet+   -  Reputation: 3626

Like
0Likes
Like

Posted 04 April 2014 - 05:46 AM


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



#5 BitMaster   Crossbones+   -  Reputation: 4275

Like
6Likes
Like

Posted 04 April 2014 - 05:54 AM

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.

#6 haegarr   Crossbones+   -  Reputation: 4438

Like
5Likes
Like

Posted 04 April 2014 - 05:55 AM

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.


Edited by haegarr, 04 April 2014 - 05:56 AM.


#7 BaneTrapper   Members   -  Reputation: 1232

Like
0Likes
Like

Posted 04 April 2014 - 05:58 AM

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!


Edited by BaneTrapper, 04 April 2014 - 06:02 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#8 Lactose!   GDNet+   -  Reputation: 3626

Like
0Likes
Like

Posted 04 April 2014 - 06:41 AM

I see, that makes sense.

Thanks for clarifying.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS