我真的不知道标题要写什么,但是基本上我只有一个.cpp,只包含标准库头,没有“using”关键字。我做了自己的“generate(...)”函数。包含库之后,Visual Studio会向我显示错误(正在调用函数),基本上是说它不知道选择std::generate(...)还是generate(...),因为它们具有匹配的参数列表。

这是错误还是我错过了什么?我可能还要补充一点,我正在使用VS2015。

#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>

template<typename Iter, typename Function>
Function generate(Iter begin, Iter end, Function f)
{
    while (begin != end)
    {
        *begin = f();
        ++begin;
    }
    return f;
}

class Random
{
public:
    Random(int low, int high)
        : mLow(low), mHigh(high)
    {}

    int operator()()
    {
        return mLow + rand() % (mHigh - mLow + 1);
    }

private:
    int mLow;
    int mHigh;
};

class Print
{
    void operator()(int t)
    {
        std::cout << t << " ";
    }
};

int main()
{
    srand(time(0));

    std::vector<int> intVec;
    intVec.resize(15);

    Random r(2, 7);
    generate(intVec.begin(), intVec.end(), r);
}

错误输出:
1>------ Build started: Project: Functor, Configuration: Debug Win32 ------
1>  Main.cpp
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(44): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): error C2668: 'generate': ambiguous call to overloaded function
1>  c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(7): note: could be 'Function generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(Iter,Iter,Function)'
1>          with
1>          [
1>              Function=Random,
1>              Iter=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: or       'void std::generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(_FwdIt,_FwdIt,_Fn0)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,
1>              _Fn0=Random
1>          ]
1>  c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): note: while trying to match the argument list '(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, Random)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

这不仅发生在VC++(VS 2015)上,而且发生在g++ 4.9+上。这里的问题是棘手的Argument Dependent Lookup (Koenig Lookup)

它查看您要添加的两个迭代器,并看到std中的“generate”功能,因为这些迭代器也来自std命名空间(这是参数依赖查找的要点)。

这个问题实际上使我感到困惑:在编写自己的 tie 实现时,它对tie做了一些额外的工作。我必须称呼我为 tye ,因为Koenig Lookup导致考虑的重载在其排名中相等,从而导致类似这样的错误。

可以使用::生成前缀以从全局 namespace (::generate( vec.begin(), vec.end(), ... );)开始查找,或者以不同的方式命名。

关于c++ - Visual Studio关于函数不在全局命名空间中的警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32316381/

10-10 16:45