2016-07-22 264 views
3

我有一个需要使用GCC-4.4.7和GCC-4.9.0进行编译的项目。将模板类作为模板模板参数传递给类成员函数

我们使用的代码将模板类模板参数传递给另一个类。虽然代码在GCC-4.9.0上编译得很好,但它在GCC-4.4.7上失败。

以下是错误的再现:

#include <iostream> 
using namespace std; 

struct E 
{ 
    int a; 
    E(int b) : a(b) {} 
}; 

template<template<int B> class C, int D> struct A 
{ 
    void print() 
    { 
     E e(D); 
     cout << e.a << endl; 
    } 
    int a; 
}; 

template<int B> struct C 
{ 
    const static int a = B; 
    void print() 
    { 
     A<C, B> a; 
     a.print(); 
    } 
}; 

int main() { 
    C<3> c; 
    c.print(); 
    return 0; 
} 

论编辑:

[[email protected] ~]$ g++-4.9 Test.cpp -o Test 
[[email protected] ~]$ 
[[email protected] ~]$ g++-4.4 Test.cpp -o Test 
Test.cpp:43: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<int B> class C, int D> struct A’ 
Test.cpp:43: error: expected a class template, got ‘C<B>’ 
Test.cpp:43: error: invalid type in declaration before ‘;’ token 
Test.cpp:44: error: request for member ‘print’ in ‘a’, which is of non-class type ‘int’ 

如何纠正错误,妥善得到它与GCC-4.4.7编译?

注意:只有C++ 98标准,代码是很旧的。

回答

4

名称查找C查找注入的类名,而您使用的古代编译器不支持将其用作模板名称。

限定名称。

A< ::C,B> a; 
+0

哇!我不知道!编译器必须是古老的。谢谢你,你是一个拯救生命的人 –

相关问题