开发者

C++ My first template ever

开发者 https://www.devze.com 2022-12-18 11:49 出处:网络
Ok, which is the best to avoid ambiguity here? template <class T> inlin开发者_运维问答e void swap(T &a, T &b)

Ok, which is the best to avoid ambiguity here?

template <class T>
inlin开发者_运维问答e void swap(T &a, T &b)
{
    T c; c = a; a = b; b = c;
}
/* blah blah blah (inside of a function:) */
for (itv = vals.begin(); itv != vals.end(); ++itv)
{
    if (at < (*itv)) { swap(at, (*itv)); }
    if (at % (*itv) == 0) atadd = false;
}
/* blah blah blah */

Calling with a swap doesn't work either, as it says cannot resolve whether it is "void swap(T &,T &)", "void std::swap(_Ty &,_Ty &)" or ...

Btw, itv is a vector<int>::iterator.

Thx.


The problem is namespace std also contains swap() function and looks like you have using namespace std; somewhere earlier, so the compiler can't decide which swap() to use - yours from the global namespace or the one from namespace std.

You need to prepend the call with "::" to explicitly tell the compiler to use your swap() from the global namespace instead of from namespace std. Alternatively you could rename your swap() function or not use using namespace std;.

0

精彩评论

暂无评论...
验证码 换一张
取 消