1

I can't figure out how to access the enum class enumerators, either as a returning value or just a value, from a templatized class. As you can see in the following example I'm totally clueless. I've googled over the error messages with no luck.

Glad if you point me out to the correct syntax.

First these are the errors:

$ g++ -Wall -std=c++11 -o main.out main.cpp

main.cpp:25:1: error: need ‘typename’ before ‘C::Values’ because ‘C’ is a dependent scope

C::Values C::Get() // <-- Error here ... ^ main.cpp: In function ‘int main()’: main.cpp:35:2: error: ‘template class C’ used without template parameters

C::Values values; // <-- ... and here

^

$

And this is the complete example so it can be tested:

template<int Val>
class C
{
public:
    enum class Values{ one, two };

    C();
    Values Get();

private:
    int val;
};

template<int Val>
C<Val>::C() : val{ Val } {}

template<int Val>
C<Val>::Values C<Val>::Get() // <-- Error here ...
{
    return Values::one;
}

int main(void)
{
    C<5> aVariable;

    C::Values values; // <-- ... and here

    return 0;
}

Thank your in advanced!!

fjrg76
  • 53
  • 6

2 Answers2

1

Firstly, you need to use keyword typename for dependent type name C<Val>::Values, e.g.

template<int Val>
typename C<Val>::Values C<Val>::Get()
~~~~~~~~

Secondly, you need to specify template argument for class template C, e.g.

C<5>::Values values;
 ~~~

LIVE

And see Where and why do I have to put the “template” and “typename” keywords?

songyuanyao
  • 147,421
  • 15
  • 261
  • 354
1

You need to help compiler telling that Value is typename:

template<int Val>
typename C<Val>::Values C<Val>::Get() 

In the second case you also need to provide template argument, e.g. 0:

C<0>::Values values;

You can read this answer to Officially, what is typename for? to get more explanations.

Yola
  • 16,575
  • 11
  • 57
  • 92
  • Thank you @Yola. Your solution works as is! ASAP I'll read the documents you've pointed me out to figure out why the typaneme keyword is needed and the <0> place holder. – fjrg76 Mar 21 '18 at 03:15