相关疑难解决方法(0)

为什么这个C++模板代码没有编译?

有谁知道为什么这不会编译?我已经尝试了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: …
Run Code Online (Sandbox Code Playgroud)

c++ templates compiler-errors

6
推荐指数
1
解决办法
8518
查看次数

标签 统计

c++ ×1

compiler-errors ×1

templates ×1