STL类编码迭代函数
-
28-09-2019 - |
题
我正在通过“使用C ++定价”中的一些C ++代码 - 使用C ++定价的书。以下代码是一个小片段,这些片段剥离了许多细节,这些细节基本上试图定义 SimplePropertySet
旨在包含名称和列表的类。
#include <iostream>
#include <list>
using namespace::std;
template <class N, class V> class SimplePropertySet
{
private:
N name; // The name of the set
list<V> sl;
public:
typedef typename list<V>::iterator iterator;
typedef typename list<V>::const_iterator const_iterator;
SimplePropertySet(); // Default constructor
virtual ~SimplePropertySet(); // Destructor
iterator Begin(); // Return iterator at begin of composite
const_iterator Begin() const;// Return const iterator at begin of composite
};
template <class N, class V>
SimplePropertySet<N,V>::SimplePropertySet()
{ //Default Constructor
}
template <class N, class V>
SimplePropertySet<N,V>::~SimplePropertySet()
{ // Destructor
}
// Iterator functions
template <class N, class V>
SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()//<--this line gives error
{ // Return iterator at begin of composite
return sl.begin();
}
int main(){
return(0);//Just a dummy line to see if the code would compile
}
在VS2008上编译此代码时,我获得以下错误:
warning C4346: 'SimplePropertySet::iterator' : dependent name is not a type
prefix with 'typename' to indicate a type
error C2143: syntax error : missing ';' before 'SimplePropertySet::Begin'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
我在这里出错或忘记了愚蠢或基本的东西吗?是语法错误吗?我无法将手指放在上面。该代码段的书籍说他们的代码是在Visual Studio 6上编译的。这是与版本有关的问题吗?
谢谢。
解决方案
如编译器所示,您必须替换:
template <class N, class V>
SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()
和 :
template <class N, class V>
typename SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()
看 这个链接 有关依赖名称的说明。
不隶属于 StackOverflow