嵌套模板gcc编译器4.1.2错误
Nested templates gcc compiler 4.1.2 error
我正在尝试创建一个模板类来将用户与数据类型隔离开来。我更喜欢使用适配器类,但需要更改的函数签名需要一个模板。
在下面的代码示例中(不是实际的项目,只是说明问题的简化版本),而在主例程中,我可以使用ob_traits接口。但是,当我尝试创建使用ob_traits作为基类的模板化StructWrapper时,我会遇到错误,gcc无法识别创建的IntAdapter类。这在MSVC 8.0上编译,但在gcc 4.1.2 20070626(Red hat 4.1.2-14)上失败
因此,首先有两个问题,你明白为什么编译失败并出现下面指定的错误吗?
第二,关于如何以更简单的方式实施这一概念,有什么建议吗?
#include <iostream>
template <typename T >
struct ob_traits
{
ob_traits( T& param ) { value = param; };
T value;
};
struct GeneralStructure
{
int a;
GeneralStructure(int param):a(param){}
};
struct DifferentStructure
{
GeneralStructure hidden;
DifferentStructure( int param ):hidden(param){};
}
;
/*template< typename T > struct ob_traits
{
};
*/
template<> struct ob_traits< GeneralStructure >
{
struct IntAdapter
{
IntAdapter( GeneralStructure& valueParam ):value(valueParam){}
GeneralStructure value;
int& getValue() { return value.a; };
};
};
template<> struct ob_traits< DifferentStructure >
{
struct IntAdapter
{
IntAdapter( DifferentStructure& valueParam):value( valueParam ){}
DifferentStructure value;
int& getValue( ){ return value.hidden.a; };
};
void dump()
{
DifferentStructure testLocal(44);
IntAdapter local( testLocal );
std::cout << local.getValue()<<std::endl;
}
};
template <typename T > struct StructWrapper:public ob_traits< T >
{
StructWrapper(){};
/*main.cpp:60: error: 'IntAdapter' was not declared in this scope
main.cpp:60: error: expected `;' before 'inner'
main.cpp:60: error: 'inner' was not declared in this scope
*/
void dumpOuter(const T& tempParam) { IntAdapter inner(tempParam); inner.dump(); };
/*
main.cpp: In member function 'void StructWrapper<T>::dumpOuterFailsAsWell(const T&)':
main.cpp:66: error: expected `;' before 'inner'
main.cpp:66: error: 'inner' was not declared in this scope
*/
void dumpOuterFailsAsWell(const T& tempParam) { ob_traits<T>::IntAdapter inner(tempParam); inner.dump(); };
};
int main(int argc, char* argv[])
{
GeneralStructure dummyGeneral(22);
ob_traits<struct GeneralStructure >::IntAdapter test(dummyGeneral);
DifferentStructure dummyDifferent(33);
ob_traits<struct DifferentStructure >::IntAdapter test2(dummyDifferent);
std::cout << "GeneralStructure: "<<test.getValue()<<std::endl;
std::cout << "DifferentStructure: "<<test2.getValue()<<std::endl;
ob_traits<struct DifferentStructure > test3;
test3.dump();
std::cout << "Test Templatedn";
return 0;
}
dumpOuter
失败,因为IntAdapter
需要合格(如参考问题中所述)。dumpOuterFailsAsWell
失败是因为GCC解析这个代码,尽管它还不完整,所以它需要知道它是一个你所说的类型:
void dumpOuterWorks(const T& tempParam)
{
typename ob_traits<T>::IntAdapter inner(tempParam);
inner.dump();
}
如果没有typename
,GCC将假定IntAdapter是一个标识符,并期望您形成一个表达式而不是变量声明。
还要注意,您不必在方法体后面放分号!
StructWrapper
继承自主类模板(即最不专业的),该模板没有定义IntWrapper,因此不能在此类中使用。我不确定用更专业的类型之一实例化StructWrapper
是否能让它工作,或者它在编译类定义本身时是否失败。
编译失败,因为IntAdapter只出现在专用模板中,因此在引用点不可见。
不清楚你会用它做什么?请澄清情况。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- C++2440错误-编译器认为字符串是常量字符
- Clang 错误 – 编译器错误或缺少一些细节?
- 视觉C++ 2017 错误?编译器优化表达式
- 配置:错误:C++编译器无法创建可执行文件
- PHP PDO_ODBC配置:错误:C 编译器无法创建可执行文件
- 致命错误 C1001:内部编译器错误(编译器文件"MSC1.cpp",第 1794 行)
- 错误:编译器限制达到简单hello world程序的堆限制
- c++链接错误:编译器找不到函数的定义
- 生成 CImg 时出现致命错误(编译器限制)
- 抛出对齐类型时出现 Clang 运行时错误.编译器错误