简体   繁体   中英

How to get this template class to compile?

Sorry, I couldn't frame a question that could capture my problem properly. My problem is this.

I have a templated class like this. I am not able to understand how exactly to define the Get function.

template<class Data>
class Class
{
    struct S
    {
    };
    void Do();
    S Get();
};

template<class Data>
void Class<Data>::Do()
{
}

template<class Data>
Class<Data>::S Class<Data>::Get()
{
}

I get the following errors

1>error C2143: syntax error : missing ';' before 'Class<Data>::Get'
1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class Data>
Class<Data>::S Class<Data>::Get()

needs to be

template<class Data>
typename Class<Data>::S Class<Data>::Get()

because S is a dependent type. Any time you have a type that's nested in a template you need to use the keyword typename . For example, an iterator over a vector<int> has the type typename vector<int>::iterator .

A C++11 style, easier to read and write :

template<class Data>
auto Class<Data>::Get() -> S {
    return {};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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