Ambiguous Call to Overloaded Function

Started by
6 comments, last by Programmer K 14 years, 11 months ago
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!
/******* DK *******/
Advertisement
**This is in the cpp file**
namespace newspace {     void func(const customtype1& t1){}    void func(const customtype2& t2){}}
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?
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.
/******* DK *******/
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.
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;
}
/******* DK *******/
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
Thank you everyone! That helped tremendously!
/******* DK *******/

This topic is closed to new replies.

Advertisement