Reputation: 465
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
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
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
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