简体   繁体   English

VC ++ 6项目迁移到VC ++ 2008时的模板化类编译错误

[英]templated class compilation errors when VC++ 6 project migrated to VC++ 2008

I have a class, see below, which compiled fine in a VC++ v6 project but when migrated to VS2008 I get compilation errors (shown below code below). 我有一个类,请参见下文,该类在VC ++ v6项目中可以很好地进行编译,但是当移植到VS2008时,我会遇到编译错误(如下代码所示)。

template<class T> class my_enum_test 
{
public:
    enum EState { STARTING, RUNNING, STOPPING, STOPPED, IDLE};

    my_enum_test(T* object) : object_(object), state_(IDLE) {}

    my_enum_test<T>::EState get_state() const;

private:
    EState state;      // execution state
    T* object_;
};


template <class T>
my_enum_test<T>::EState my_enum_test<T>::get_state(
    ) const
{
    return state;
}


int main() {

    my_enum_test<int> my_test;
    my_enum_test<int>::EState the_state = my_test.get_state();

    return 0;
}

errors: 错误:

Compiling...
main.cpp
main.cpp(9) : warning C4346: 'my_enum_test::EState' : dependent name is not a type
        prefix with 'typename' to indicate a type
        main.cpp(14) : see reference to class template instantiation 'my_enum_test'    being compiled
main.cpp(9) : error C2146: syntax error : missing ';' before identifier 'get_state'
main.cpp(9) : error C2875: using-declaration causes a multiple declaration of 'EState'
main.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(9) : warning C4183: 'get_state': missing return type; assumed to be a member function returning 'int'
main.cpp(18) : warning C4346: 'my_enum_test::EState' : dependent name is not a type
        prefix with 'typename' to indicate a type
main.cpp(18) : error C2143: syntax error : missing ';' before 'my_enum_test::get_state'
main.cpp(18) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Results

Ignoring the fact that the class doesn't do anything, how can I fix the compilation problems? 忽略该类不执行任何操作的事实,如何解决编译问题?

Since you have a qualified, dependent type name ( EState ), you need to use the typename disambiguator: 既然你有一个合格的,依赖型名称( EState ),您需要使用typename消歧:

    template <class T>
    typename my_enum_test<T>::EState my_enum_test<T>::get_state(
//  ^^^^^^^^
        ) const
    {
        return state;
    }

This way the compiler will know that EState is the name of a type, rather than the name of a static member variable. 这样,编译器将知道EState是类型的名称,而不是静态成员变量的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM