gpt4 book ai didi

c++ - 为什么这个 C++ 模板代码不能编译?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:26 25 4
gpt4 key购买 nike

有谁知道为什么这不会编译?我已经尝试过 VS 2008 和 GCC 4.something 并且都吐出错误。我是否引用“ThisFunctionDoesNotCompile()”并不重要。

我可以通过将“InternalType”作为第二个模板参数传递给 Base 来解决这个问题,但我仍然很好奇为什么这会出现错误。

#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;
}

这些是我在 VS 2008 中遇到的错误:

1>e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C2039: 'InternalType' : is not a member of 'Derived<InInternalType>'
1> with
1> [
1> InInternalType=DataClass
1> ]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(35) : see reference to class template instantiation 'Base<DerivedType>' being compiled
1> with
1> [
1> DerivedType=Derived<DataClass>
1> ]
1> e:\test\generaltestprogram\generaltestprogram\main.cpp(58) : see reference to class template instantiation 'Derived<InInternalType>' being compiled
1> with
1> [
1> InInternalType=DataClass
1> ]
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C2146: syntax error : missing ';' before identifier 'ThisFunctionDoesNotCompile'
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\test\generaltestprogram\generaltestprogram\main.cpp(28) : warning C4183: 'ThisFunctionDoesNotCompile': missing return type; assumed to be a member function returning '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>'

最佳答案

当模板类 Base 被实例化为类 Derived 的父类时,Derived 类不是一个完整的类型。

Base<Derived<DataClass> >Derived<DataClass> 的父类, 它必须在 Derived<DataClass> 之前被实例化可以实例化。所以当课Base<Derived<DataClass> >由模板构建,Derived<DataClass>表现得好像它是一个前向声明。您可能已经知道,您不能引用不完整类型的成员,您也不能前向声明嵌套类型,所以您在这里运气不好。

顺便说一句,这就是为什么很难使用模板实现适当协变的 clone() 方法。参见 herehere (我的)。

关于c++ - 为什么这个 C++ 模板代码不能编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1364837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com