Jump to content
  • Advertisement
Sign in to follow this  
Programmer K

Ambiguous Call to Overloaded Function

This topic is 3501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm having a problem with two functions that do the same thing but for different class types. The problem occurred after I added the functions to a namespace. Please review the following code to see if you can help me. Thanks! BEFORE: **This is in the header file** void func(const customtype1& t1); void func(const customtype2& t2); **This is in the cpp file** void func(const customtype1& t1){} void func(const customtype2& t2){} AFTER: **This is in the header file** namespace newspace{ void func(const customtype1& t1); void func(const customtype2& t2); } **This is in the cpp file** using namespace newspace; void func(const customtype1& t1){} void func(const customtype2& t2){} It says ambiguous call to overloaded function. I don't know what to do. Help!

Share this post


Link to post
Share on other sites
Advertisement
**This is in the cpp file**

namespace newspace {

void func(const customtype1& t1){}
void func(const customtype2& t2){}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Programmer K
AFTER:

**This is in the cpp file**
using namespace newspace;
void func(const customtype1& t1){}
void func(const customtype2& t2){}


That did not define either version of newspace::func; instead, it simply said "if you can't find a name in the global namespace, try the 'newspace' namespace too", and then defined func in the global namespace (for both types). See the above post for how to fix this.

But could we see where the function is called?

Share this post


Link to post
Share on other sites
OK, so are you saying that I can't have a declaration and a definition of the functions? Meaning they have to be declared and defined in the same region.

Share this post


Link to post
Share on other sites
Quote:
Original post by Programmer K
OK, so are you saying that I can't have a declaration and a definition of the functions? Meaning they have to be declared and defined in the same region.


Of course you can. We just pointed out that you have to declare and define them in the same namespace. Namespaces can, and usually are, split across many files.

Still, you haven't shown the code that causes the error.

Share this post


Link to post
Share on other sites
Ok, the code.


**vector.h**
namespace myengine{
float Magnitude(const vector2d& v2d);
void Normalize(vector2d& v2d);
vector2d Normalized(const vector2d& v2d);
}

**vector.cpp**
#include "vector.h"
using namespace myengine;

float Magnitude(const vector2d& v2d)
{
float temp = v2d.x * v2d.x + v2d.y * v2d.y;
return sqrt(temp);
}

void Normalize(vector2d& v2d)
{
float temp = Magnitude(v2d);
v2d.x /= temp;
v2d.y /= temp;
}

vector2d Normalized(const vector2d& v2d)
{
vector2d temp = v2d;
Normalize(temp);
return temp;
}

Share this post


Link to post
Share on other sites
Like the others have said, you need to wrap your definitions in your namespace.


**vector.h**
namespace myengine{
float Magnitude(const vector2d& v2d);
void Normalize(vector2d& v2d);
vector2d Normalized(const vector2d& v2d);
}

**vector.cpp**
#include "vector.h"

namespace myengine{
float Magnitude(const vector2d& v2d)
{
float temp = v2d.x * v2d.x + v2d.y * v2d.y;
return sqrt(temp);
}

void Normalize(vector2d& v2d)
{
float temp = Magnitude(v2d);
v2d.x /= temp;
v2d.y /= temp;
}

vector2d Normalized(const vector2d& v2d)
{
vector2d temp = v2d;
Normalize(temp);
return temp;
}
}



Also: the [source][/source] tags are your friend.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!