I have the following code in a .h
file:
class Test1 {
struct A1 {
int x;
};
static const A1 a1;
};
template <class T>
class Test2 {
struct A2 {
int x;
};
static const A2 a2;
};
and I define values for both a1
and a2
in a .cpp
file:
const Test1::A1 Test1::a1 = { 5 };
template<class T>
const Test2<T>::A2 Test2<T>::a2 = { 5 };
The odd thing is that for Test1
everything works, but for Test2
I get the following error in the last line of the .cpp
file:
Error C2061 syntax error: identifier 'A2'
The only difference between Test1
and Test2
is that Test2
has a template. Why is this a problem, and how can I fix it? Also, this works as well:
test.h
struct A3 {
int x;
};
template <class T>
class Test3 {
static const A3 a3;
};
test.cpp
template<class T>
const A3 Test3<T>::a3 = { 5 };
So the problem is with using a struct defined in a templated class, but I can't find out what exactly is the problem.
(I'm compiling with c 14.)
CodePudding user response:
The fix is suggested by a compiler, just be more careful in reading the compiler messages.
<source>(17): warning C4346: 'A2': dependent name is not a type
<source>(17): note: prefix with 'typename' to indicate a type
<source>(17): error C2061: syntax error: identifier 'A2'
<source>(17): error C2143: syntax error: missing ';' before '{'
<source>(17): error C2447: '{': missing function header (old-style formal list?)
Or
<source>:17:7: error: missing 'typename' prior to dependent type name 'Test2<T>::A2'
const Test2<T>::A2 Test2<T>::a2 = { 5 };
^~~~~~~~~~~~
typename
1 error generated.
That means the correct code:
template<class T>
const typename Test2<T>::A2 Test2<T>::a2 = { 5 };
Read more: Why do we need typename here?.