c++ - GCC 模板 : expected »(« before »>« token

标签 c++ templates gcc

<分区>

我不明白为什么下面的代码不能编译:

template< typename TypeArg >
class Data
{
public:
    struct Selector1
    {
    };

    template< typename Selector >
    void foo()
    {
    }
};

template< typename TypeArg >
class Test
{
public:
    void test();
};


template< typename TypeArg >
void Test< TypeArg >::test()
{
    Data< TypeArg > data;
    data.foo< typename Data< TypeArg >::Selector1 >();
}

我已经使用 GCC 4.6 和 GCC 4.9 对其进行了测试。两者都给我同样的错误:

test.cpp: In member function »void Test::test()«: test.cpp:28:51: Error: expected »(« before »>« token test.cpp:28:53: Error: expected primary-expression before »)« token

谁能告诉我编译代码需要做什么?

最佳答案

由于 data 的类型是依赖的,data.foo 的性质是未知的,需要消除歧义:

data.template foo<typename Data< TypeArg >::Selector1>();
//   ^^^^^^^^

关于c++ - GCC 模板 : expected »(« before »>« token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318881/

相关文章:

C++:如何在不使用库的情况下序列化/反序列化对象?

c++ - 在嵌套私有(private)类内部的外部类中使用 C++ 公共(public)变量

c++ - 模板函数静态变量

c++ - 模板文件中的类 'does not name a type' 错误

c++ - 在遍历字符串时更改字符会使程序崩溃

c++ - 我的 is_complete 类型特征的实现是否暴露了编译器错误?

java - 声明一个变量但不初始化它会提高性能吗?

javascript - 使用js脚本生成<head>和&lt;footer&gt;

c++ - 模板类成员函数的显式特化

c++ - std::span 是 View 吗?