小编Seb*_*ian的帖子

嵌套模板

我有一个C++模板的奇怪问题,我不明白为什么以下代码不起作用:

#include <iostream>

template <typename A, typename B>
class TypePair {
public:
    typedef A First;
    typedef B Second;
};


template <typename P>
class Foo {
    public:
        Foo(P::First f, P::Second) {
            std::cout
                << "first = " << f << std::endl
                << "second = " << s << std::endl;
        }
};


int main(int argc, char **argv) {
    Foo<TypePair<int, double> > foo(42, 23.0);

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

该代码产生以下错误:

$ g++ templates.cpp -o templates
templates.cpp:14: error: expected ‘)’ before ‘f’
templates.cpp: In function ‘int main(int, …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
1
解决办法
3905
查看次数

标签 统计

c++ ×1

templates ×1