C++ 为什么';t这是C++;模板代码编译?
有人知道为什么这个不能编译吗?我试过VS2008和GCC4.something,但都有错误。我是否引用“ThisFunctionDoesNotCompile()”并不重要 我可以通过将“InternalType”作为第二个模板参数传递给Base来解决这个问题,但我仍然很好奇为什么会出现错误C++ 为什么';t这是C++;模板代码编译?,c++,templates,compiler-errors,C++,Templates,Compiler Errors,有人知道为什么这个不能编译吗?我试过VS2008和GCC4.something,但都有错误。我是否引用“ThisFunctionDoesNotCompile()”并不重要 我可以通过将“InternalType”作为第二个模板参数传递给Base来解决这个问题,但我仍然很好奇为什么会出现错误 #include <iostream> using namespace std; class DataClass { public: int m_data; }; template&l
#include <iostream>
using namespace std;
class DataClass
{
public:
int m_data;
};
template<typename DerivedType>
class Base
{
public:
int ThisFunctionCompiles()
{
// No problems here.
typename DerivedType::InternalType temp;
temp.m_data = 5;
return temp.m_data;
}
// error C2039: 'InternalType' : is not a member of 'Derived<InInternalType>'
typename DerivedType::InternalType ThisFunctionDoesNotCompile()
{
return static_cast<DerivedType*>(this)->GetInternalData();
}
};
template<typename InInternalType>
class Derived : public Base<Derived<InInternalType> >
{
public:
typedef InInternalType InternalType;
InternalType GetInternalData()
{
return m_internalData;
}
private:
InternalType m_internalData;
public:
void SetInternalData( int newVal )
{
m_internalData.m_data = newVal;
}
};
int main()
{
Derived<DataClass> testDerived;
testDerived.SetInternalData( 3 );
cout << testDerived.GetInternalData().m_data << endl;
cout << testDerived.ThisFunctionCompiles() << endl;
// The compiler gives an error regardless of whether or not this is commented out.
//cout << testDerived.ThisFunctionDoesNotCompile().m_data << endl;
return 0;
}
#包括
使用名称空间std;
类数据类
{
公众:
int m_数据;
};
模板
阶级基础
{
公众:
int ThisFunctionCompiles()
{
//这里没问题。
typename-DerivedType::InternalType-temp;
温度m_数据=5;
返回temp.m_数据;
}
//错误C2039:“InternalType”:不是“派生”的成员
typename DerivedType::InternalType此函数不可编译()
{
返回static_cast(this)->GetInternalData();
}
};
模板
派生类:公共基
{
公众:
内部类型内部类型内部类型的类型定义;
InternalType GetInternalData()
{
返回m_内部数据;
}
私人:
InternalType m_internalData;
公众:
void SetInternalData(int newVal)
{
m_internalData.m_data=newVal;
}
};
int main()
{
派生测试派生;
testDerived.SetInternalData(3);
cout]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(35):请参阅对正在编译的类模板实例化“Base”的引用
1> 与
1> [
1> DerivedType=派生
1> ]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(58):请参阅对正在编译的类模板实例化“派生”的引用
1> 与
1> [
1> InInternalType=DataClass
1> ]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(27):错误C2146:语法错误:在标识符“ThisFunctionDoesNotCompile”之前缺少“;”
1>E:\Test.EngalTestPosith\GualalTestStudio \Maul.CPP(27):错误C44 30:缺少类型说明符-int假设。C++:不支持默认int
1>E:\Test.EngalTestPosith\GualalTestStudio \Maul.CPP(28):错误C44 30:缺少类型说明符-int假设。C++:不支持默认int
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(28):警告C4183:“ThisFunctionDoesNotCompile”:缺少返回类型;假定为返回“int”的成员函数
以下是GCC给我的:
main.cpp: In instantiation of 'Base<Derived<DataClass> >':
main.cpp:96: instantiated from 'Derived<DataClass>'
main.cpp:119: instantiated from here
main.cpp:88: error: no type named 'InternalType' in 'class Derived<DataClass>'
main.cpp:在“Base”的实例化中:
main.cpp:96:从“派生”实例化
main.cpp:119:从此处实例化
main.cpp:88:错误:“类派生”中没有名为“InternalType”的类型
将模板化的类基实例化为派生类的父类时,派生类不是完整类型
由于Base
是Derived
的父类,因此必须先实例化它,然后才能实例化Derived
。因此,当从模板构建类Base
时,Derived
的行为就好像它是一个转发声明一样。您可能知道,您不能引用不完整类型的成员s、 您的转发也不能声明嵌套类型,因此您在这里运气不佳
顺便说一句,这就是为什么很难使用模板实现正确的协变clone()方法的原因。请参见和(我的)
main.cpp: In instantiation of 'Base<Derived<DataClass> >':
main.cpp:96: instantiated from 'Derived<DataClass>'
main.cpp:119: instantiated from here
main.cpp:88: error: no type named 'InternalType' in 'class Derived<DataClass>'