当前位置: 技术问答>linux和unix
关于模板实例化和STL::map
来源: 互联网 发布时间:2016-12-16
本文导语: 我有一个类定义如下: template class EntityDatabase { public: typedef std::map container;//错误相关的地方 // -------------------------------------------------------------------- // The inner iterator class, used to iterate through...
我有一个类定义如下:
为什么编译报如下错?
用的是code::blocks gcc 4.5
template
class EntityDatabase
{
public:
typedef std::map container;//错误相关的地方
// --------------------------------------------------------------------
// The inner iterator class, used to iterate through the database.
// --------------------------------------------------------------------
class iterator : public container::iterator
{
public:
// --------------------------------------------------------------------
// NOTE: the constructors are needed as a result of VC6 sucking.
// Have I mentioned that VC6 sucks yet?
// --------------------------------------------------------------------
iterator() {}; // default constructor
iterator( const container::iterator& p_itr ) // copy constructor
{
container::iterator& itr = *this; //报错的地方
itr = p_itr;
}
// --------------------------------------------------------------------
// "dereferences" the iterator to return a reference to the
// object that it points to.
// --------------------------------------------------------------------
inline datatype& operator*()
{
container::iterator& itr = *this; // also needed because VC6 sucks
return itr->second;
}
// --------------------------------------------------------------------
// the "pointer-to-member" operator, which will allow you to use it on
// iterators like this: itr->Function();
// --------------------------------------------------------------------
inline datatype* operator->()
{
container::iterator& itr = *this; // also needed because VC6 sucks
return &(itr->second);
}
}; // end iterator inner class
// --------------------------------------------------------------------
// returns an iterator pointing to the first item
// --------------------------------------------------------------------
inline static iterator begin()
{
return iterator( m_map.begin() );
}
。。。。。。
}
为什么编译报如下错?
/home/chacha/MUD/Demos/Chapter08/Demo08-01/SimpleMUD/EntityDatabase.h|46|error: need ‘typename’ before ‘SimpleMUD::EntityDatabase::container::iterator’ because ‘SimpleMUD::EntityDatabase::container’ is a dependent scope|
/home/chacha/MUD/Demos/Chapter08/Demo08-01/SimpleMUD/EntityDatabase.h|46|error: ISO C++ forbids declaration of ‘p_itr’ with no type|
。。。。。。
用的是code::blocks gcc 4.5
|
template是模板声明,声明完后T就相当于一个参数类型(int,double。。),
可以这么简单的理解,在以后的代码中,T将被置换成你传给类的参数类型
EntityDatabase 里面所有的T会被替换成int。
可以这么简单的理解,在以后的代码中,T将被置换成你传给类的参数类型
EntityDatabase 里面所有的T会被替换成int。