Jake B.
Jake B.

Reputation: 465

Dealing with ambiguous declarations in C++

In my header file I define:

inline t_pair pair(int a, int b) { t_pair p; p.a = a; p.b = b; return p; }

But I get a compiler error "Reference to 'pair' is ambiguous". Apparently there is a

struct _LIBCPP_TYPE_VIS_ONLY pair

defined in utility.cpp, which I do not include directly.

Is there a way to still use my pair function without renaming it?

Upvotes: 2

Views: 10373

Answers (3)

k170
k170

Reputation: 383

As already mentioned, you can wrap your function declaration with another namespace or you can just use the std::pair class and avoid reinventing the wheel.

Also note that the std::pair class allows you to create pairs of generic types. So it's not limited to only pairs of type int. You can find an example of its usage here.

Upvotes: 3

ForEveR
ForEveR

Reputation: 55887

You can (and probably should) put your function in namespace, but, you actually should to remove using namespace std from your code, since there is class pair in std namespace. You can not include utility (where pair is defined), but it can be included by some other file.

Upvotes: 0

Anton Poznyakovskiy
Anton Poznyakovskiy

Reputation: 2169

Yes, by putting your function declaration into a namespace:

namespace MyNamespace
{
    inline t_pair pair(int a, int b) { t_pair p; p.a = a; p.b = b; return p; }
}

And then calling it like MyNamespace::pair (a, b).

Upvotes: 1

Related Questions