c++ - 从函数返回::迭代器不编译

标签 c++ templates tuples

<分区>

以下 repo 编译正常:

#include <tuple>
#include <vector>

class Foo
{
public:

    typedef std::tuple<int, int> foo_type;

    std::vector<foo_type>::iterator Find() {
        return Listeners.begin();
    }

    std::vector<foo_type> Listeners;
};

int main() 
{
    Foo foo;

    auto i = foo.Find();
}

但是当我在 P、Q 上模板化元组时,如以下 repo 所示,我收到此错误:

syntax error : missing ';' before identifier 'Find'

#include <tuple>
#include <vector>

template<typename P, typename Q>
class Foo
{
public:

    typedef std::tuple<P, Q> foo_type;

    std::vector<foo_type>::iterator Find() {
        return Listeners.begin();
    }

    std::vector<foo_type> Listeners;
};

int main()
{
    Foo<int, int> foo;

    auto i = foo.Find();
}

最佳答案

你在这里遗漏了一个typename:

typename std::vector<foo_type>::iterator Find() {
^^^^^^^^
    return Listeners.begin();
}

有关更详细的说明,请参阅 Where and why do I have to put the "template" and "typename" keywords?

关于c++ - 从函数返回::迭代器不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636796/

相关文章:

c++ - 连接一元参数的模板参数包

stack - OCaml 元组栈

python - 将列表中的 str 元素耦合到元组列表

c++ - Opengl-es 2.0中渲染三角形的问题

javascript - meteor 在模板空格键中显示数组的第一个元素

c++ - 来自特定位置的 upper_bound 函数而不是 data.begin()

c++ - 基于位置的包中的特定类型

ios - Swift 元组到可选赋值

c++ - D3DXLoadSurfaceFromFile 导致程序过早关闭

c++ - 在opencv中从HSV中查找颜色