Dirt
Dirt

Reputation: 331

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

Upvotes: 0

Views: 73

Answers (2)

galop1n
galop1n

Reputation: 8824

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

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

Upvotes: 0

Adam
Adam

Reputation: 17389

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.

Upvotes: 3

Related Questions