你大概是using namespace std;
。
在这种情况下,编译器不知道该选择什么,因为它使所有std::
成员都可用而无需自动键入,这两个函数都适用:
using namespace std;
swap(a, b); //your swap
swap(a, b); //std::swap
在这种情况下,您有严格的函数调用:
std::swap(a, b); //from std
swap(a, b); // your one
这实际上是一个很好的例子,说明了为什么应该避免using namespace std
. 祝你好运!
更新:这可能是您的解决方案 - 将您swap()
的std::sort()
使用范围移出:
#include <algorithm>
#include <vector>
namespace detail
{
struct someContainer
{
someContainer(int &v)
{
value = v;
}
int value;
someContainer &operator = (const someContainer & rhs)
{
this->value = rhs.value;
}
bool operator == (someContainer &rhs) const
{
return this->value == rhs.value;
}
bool operator <= (someContainer &rhs) const
{
return this->value <= rhs.value;
}
bool operator >= (someContainer &rhs) const
{
return this->value >= rhs.value;
}
bool operator > (someContainer &rhs) cosnt
{
return this->value > rhs.value;
}
bool operator < (someContainer &rhs) const
{
return this->value < rhs.value;
}
};
void doSomeStuff()
{
std::vector<someContainer> vec;
for (int i = 0; i < vec.size(); ++i)
{
vec.push_back(someContainer(i));
}
std::sort(vec.begin(), vec.end());
}
}
namespace mySwap
{
template< class T >
void swap(T &a, T &b)
{
T c = a;
a = b;
b = c;
}
}
int main()
{
detail::doSomeStuff();
return 0;
}