Visual Studio 可以很好地编译这段代码,但 gcc 只允许它在没有模板运算符的情况下进行编译。使用模板运算符会出现以下错误:
第 29 行:错误:应为 `;'在“itrValue”之前
class Test
{
public:
Test& operator<<(const char* s) {return *this;} // not implemented yet
Test& operator<<(size_t s) {return *this;} // not implemented yet
Test& operator<< (const std::list<const char*>& strList)
{
*this << "count=" << strList.size() << "(";
for (std::list<const char*>::const_iterator itrValue = strList.begin();
itrValue != strList.end(); ++itrValue)
{
*this << " " << *itrValue;
}
*this << ")";
return *this;
}
template <class T>
Test& operator<< (const std::list<T>& listTemplate)
{
*this << "count=" << listTemplate.size() << "(";
// this is line 28, the next line is the offending line
for (std::list<T>::const_iterator itrValue = listTemplate.begin();
itrValue != listTemplate.end(); ++itrValue)
{
*this << " " << *itrValue;
}
*this << ")";
return *this;
}
};
最佳答案
GCC 是对的,const_iterator 是一个类型,并且模板依赖于模板运算符<<,你需要告诉编译器它是一个类型而不是变量:
typename std::list<T>::const_iterator
关于c++ - GCC模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/389797/