2

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

#include <iostream>
using namespace std;

template <class T>
class Test
{
    union obj
    {
        union obj* next;
        int num;
    };

    static const int SZ=3;
    static obj* volatile list[SZ];
};

template <class T>
Test<T>::obj* volatile
Test<T>::list[SZ]=
{
    0, 0, 0
};

int main()
{
    return 0;
}

With g++, the error I get is:

18|error: expected constructor, destructor, or type conversion before '*' token
4
  • does adding typename before Test<T>::obj* helps? Commented Apr 8, 2012 at 4:12
  • Does it work if you declare that union outside of the class? Commented Apr 8, 2012 at 4:13
  • Both are ok,but I just know the latter. Commented Apr 8, 2012 at 7:15
  • 1
    Closing this question is unjustified and unhelpful. Two questions may have the same answer but not be a duplicate. I was searching for how to initialize a static array of a templated class. The apparent duplicate question somewhat assumes I already knew the answer. Commented Nov 7, 2012 at 11:52

1 Answer 1

3

Add the keyword typename before Test<T>::obj* in the definition of the member.

Sign up to request clarification or add additional context in comments.

4 Comments

@Potatoswatter I think after the Test<T>::list the names inside Test<T> are in scope. Consider: class A { class B {}; void c(B); }; void A::c(B) {}
@bames: You're actually right (sorry if you read my earlier comment). Case in point.
@bames53 bah, I deleted my comment about SZ needing qualification before looking it up in the standard. §9.4.2 [class.static.data] says "The initializer expression in the definition of a static data member is in the scope of its class (3.3.7)" but doesn't say anything about the declarator itself. Neither do I see anything under 8.4.2 [dcl.array]. So I think this might be a GCC bug.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.