0

When I use the scope resolution operator as part of a method declaration within a template struct, it won't work. But when I use the same scope resolution operator as part of a sizeof() within a method definition within the same template struct, it works fine. Can someone please help me to get this to work as part of a method definition? I'm using Visual C++ within Visual Studio. Here's a code sample of the problem.

sample.h

struct Top {
    struct Data {
        int time;
    };

    Data* pData;
};

template<typename T>
struct App {
    T* pParent;

    T::Data* getDD() {  // error C2061: syntax error: identifier 'Data'
        return pParent->pData;
    }

    int getSize() {
        return sizeof(T::Data);   // This works fine
    }
};

sample.cpp

    main() {
        Top top;
        App<Top> app;

        Top::Data* pData = app.getDD();
        int size = app.getSize();
    }
Gary G.
  • 318
  • 3
  • 12
  • 1
    You are missing `typename` before the declaration, i.e. `typename T::Data* getDD() {`. Same here: `return sizeof(typename T::Data);` – PaulMcKenzie Oct 20 '18 at 05:39
  • Thank you @PaulMcKenzie for your answer. Unfortunately I did not search for "typename" because I didn't already know that was the answer. I initially searched for "scope resolution operator" and I couldn't find my answer within those search results. The question that you referenced as duplicate does not seem to be the same question as mine but it does contain an answer to my question. – Gary G. Oct 20 '18 at 13:07
  • 1
    I know if you're not aware of `typename` you more than likely will have to really work to find the answer. So no problems from me if you had trouble finding the answer (IMO your question shouldn't be downvoted). – PaulMcKenzie Oct 20 '18 at 17:38

0 Answers0