簡體   English   中英

為什么這個C ++模板代碼沒有編譯?

[英]Why doesn't this C++ template code compile?

有誰知道為什么這不會編譯? 我已經嘗試了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()方法很困難的原因。 看到這里這里 (我的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM