相关疑难解决方法(0)

353
推荐指数
18
解决办法
30万
查看次数

无法推断出模板类型

我正在尝试将迭代器作为模板参数传递给模板方法,但编译器抱怨:

error C2783: 'void Test::Assert(std::vector<T>::const_iterator)':
could not deduce template argument for 'T'
Run Code Online (Sandbox Code Playgroud)

产生错误的代码是:

#include "stdafx.h"
#include <iostream>
#include <vector>

class Test
{
    public:
        template <typename T>
        void Assert(typename std::vector<T>::const_iterator it)
        {
            std::cout << *it << std::endl;
        }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Test test;

    std::vector<double> myVec;

    test.Assert(myVec.cbegin());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我猜有一种简单的方法可以使这项工作,因为大多数std算法可以从迭代器中推断出类型.

c++ templates template-argument-deduction

9
推荐指数
1
解决办法
1067
查看次数