我遇到以下代码的问题:
#include <list>
#include <boost/shared_ptr.hpp>
#include "Protocol/IMessage.hpp"
template <typename HeaderType>
class Connection {
public:
typedef IMessage<HeaderType> MessageType;
typedef boost::shared_ptr<MessageType> MessagePointer;
template <typename Handler>
void
FlushMessageQueue(Handler handler) {
std::list<MessagePointer>::iterator ib = message_queue_.begin(); // line 69
std::list<MessagePointer>::iterator ie = message_queue_.end();
for (; ib != ie; ++ib) {
AsyncWrite(*ib, handler);
}
}
private:
std::list<MessagePointer> message_queue_;
};
gcc(4.2.1)告诉我:
include/Network/Connection.hpp: In member function 'void Network::Connection<MT>::FlushMessageQueue(Handler)':
include/Network/Connection.hpp:69: error: expected `;' before 'ib'
我想知道为什么我不能为MessagePointer的列表创建一个迭代器。
有什么想法吗?
谢谢。
答案 0 :(得分:11)
std::list<MessagePointer>
是依赖类型(即它取决于模板参数的类型)。因此,您需要使用typename
来声明::iterator
应该是所有潜在实例化的类型(因为它们可能是某些实例化的值,如果它们是专门的)。所以:
typename std::list<MessagePointer>::iterator ib = message_queue_.begin();