STL Error Converting VS 6 Project to VS2010

2019-07-15 04:19发布

I'm converting a Visual Studio 6 project to Visual Studio 2010. The project uses STL heavily. After converting, the compiler gives an error. The code and error are as follows.

#include <list>

namespace mySpace
{

template <class T>
class MyList : public std::list<T>
{
    public:
        typedef std::list<T>::allocator_type AllocatorType;
    }

Error: Error 2 error C2146: syntax error : missing ';' before identifier 'AllocatorType' c:\myProject\mylist.h 39 1

I can click on the 'allocator_type' text and hit F12 and the IDE takes me to the 'allocator_type' definition in list.

If I remove '::allocator_type' the error goes away.

Any ideas what would cause this?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-15 05:01

It should be

typedef typename std::list<T>::allocator_type AllocatorType;

You have to tell the compiler allocator_type is actually a type.

By the way, inheriting from STL containers isn't a great practice as they don't have virtual destructors.

查看更多
小情绪 Triste *
3楼-- · 2019-07-15 05:08

Modify the typedef line as:

typedef typename std::list<T>::allocator_type AllocatorType;

to state that std::list<T>::allocatoris a type.

查看更多
登录 后发表回答