error: expected nested-name-specifier before 'writer_trait'

Jul 19, 2011 at 1:40am
the code:
1
2
3
4
5
6
7
8
9
10
11
template <typename writer_trait, typename reader_trait>
class filer_trait
{
public:
    typedef typename writer_trait writer_type;
    typedef typename reader_trait reader_type;
};

int main() {
    return 0;
}


I see stl uses this kind of "typedef typename" a lot, why this code not compile?

the full error message:

C:\Windows\system32\cmd.exe /c make
[100%] Building CXX object CMakeFiles/test.dir/test.cpp.obj
G:\workspace\test\test.cpp:12:19: error: expected nested-name-specifier before '
writer_trait'
G:\workspace\test\test.cpp:12:32: error: expected ';' before 'writer_type'
G:\workspace\test\test.cpp:13:19: error: expected nested-name-specifier before '
reader_trait'
G:\workspace\test\test.cpp:13:32: error: expected ';' before 'reader_type'


Thanks.
Last edited on Jul 19, 2011 at 1:41am
Jul 19, 2011 at 1:42am
I believe for templatized code you need to put them inside the .h aka header file to get it to compile. The reason behind is quite long and I will let other veteran C++ developers answer that.
Jul 19, 2011 at 1:50am
I tried put the template code into "test.h", and included by test.cpp, it's the same error.

1
2
3
4
5
6
7
8
9
//test.h

template <typename writer_trait, typename reader_trait>
class filer_trait
{
public:
	typedef typename writer_trait writer_type;
	typedef typename reader_trait reader_type;
};

1
2
3
4
5
//test.cpp
#include "test.h"
int main() {
	return 0;
}

I'm using MinGW g++ 4.5.2
Jul 19, 2011 at 2:18am
it seems typedef typename is MSVC style, not for g++
I just removed the "typename" and it compiles.

Thanks.
Topic archived. No new replies allowed.