-
typedef issue
Code:
template<typename Type>
class GetTypeRegister
{
public:
typedef Type::InnerClass type;//Errors on this line
};
int main()
{
}
I was wondering why this doesn't compile. This is a simplified example of something that would be very useful to me in some template work I'm doing. I wouldn't think this template would be generated at all as I never use it, and so should not generate errors. Perhaps typedef's work in a different fashion than I expect.
It generates the following errors and warnings (file paths cut for brevity).
Code:
warning C4346: 'Type::InnerClass' : dependent name is not a type
prefix with 'typename' to indicate a type
see reference to class template instantiation 'GetTypeRegister<Type>' being compiled
error C2146: syntax error : missing ';' before identifier 'type'
warning C4517: access-declarations are deprecated; member using-declarations provide a better alternative
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
-
How does it know that InnerClass is a nested type and not some variable? Add typename like it says and perhaps it will work.
-
Code:
typedef typename Type::InnerClass type;
-
Long story short, when working with anything that is a member of a template type, you must add typename before it (if it's a type).
In your example, since Type is a template type to some class or something and InnerClass is a type, add typename.
-
Thank you, I'm still figuring out the subtleties of templates.