我对C ++很新,并且使用它创建类,特别是使用模板类。我收到了很多错误消息,我不知道如何修复它们,希望有人能提供帮助,提前谢谢。
班级:
template <class T> class Vector {
public:
typedef T* iterator;
Vector();
iterator begin();
iterator end();
int size();
iterator insert(iterator position, const T& item);
void alloc_new();
private:
T items;
int used;
};
template <class T> Vector::Vector() {
items = [1000];
used = 0;
}
template <class T> iterator Vector::begin() {
return items;
}
template <class T> iterator Vector::end(){
return items + used;
}
template <class T> int Vector::size() {
return used;
}
template <class T> iterator Vector::insert(iterator position, const T& item){
if (used+1 > items){
alloc_new();}
items[position] = item;
used =+ 1;
}
template <class T> void Vector::alloc_new(){
items = used*2;
T tmp[] = items;
for (int i = 0; i < used; i++){
tmp[i] = items[i];
}
delete items;
items = tmp;
}
错误消息:
main.cpp:67: error: `template class Vector' used without template parameters main.cpp:67: error: ISO C++ forbids declaration of `Vector' with no type main.cpp:67: error: declaration of template `template int Vector()' main.cpp:52: error: conflicts with previous declaration `template class Vector' main.cpp:52: error: previous non-function declaration `template class Vector' main.cpp:67: error: conflicts with function declaration `template int Vector()' main.cpp: In function `int Vector()': main.cpp:68: error: `items' undeclared (first use this function) main.cpp:68: error: (Each undeclared identifier is reported only once for each function it appears in.) main.cpp:68: error: expected primary-expression before '[' token main.cpp:69: error: `used' undeclared (first use this function) main.cpp: At global scope: main.cpp:72: error: expected constructor, destructor, or type conversion before "Vector" main.cpp:72: error: expected `;' before "Vector" main.cpp:76: error: expected constructor, destructor, or type conversion before "Vector" main.cpp:76: error: expected `;' before "Vector" main.cpp:80: error: `template class Vector' used without template parameters main.cpp: In function `int size()': main.cpp:81: error: `used' undeclared (first use this function) main.cpp: At global scope: main.cpp:84: error: expected constructor, destructor, or type conversion before "Vector" main.cpp:84: error: expected `;' before "Vector" main.cpp:94: error: `template class Vector' used without template parameters main.cpp: In function `void alloc_new()': main.cpp:95: error: `items' undeclared (first use this function) main.cpp:95: error: `used' undeclared (first use this function)
答案 0 :(得分:1)
您需要指定模板参数。例如:
template <class T> Vector<T>::Vector() {
items = [1000]; // note, this is also invalid syntax
used = 0;
}
答案 1 :(得分:1)
Vector::vector()
需要更改为Vector<T>::vector()
。以下成员函数也是如此。iterator
需要以Vector<T>
作为Vector<T>::iterator
,因为iterator
已在类Vector
内声明答案 2 :(得分:0)
因为迭代器是Vector的嵌套类型,所以改变
template <class T> iterator Vector::begin() { return items; }
到
template <typename T> Vector<T>::iterator Vector<T>::begin() { return items; }
也可以使用涉及迭代器的所有其他方法。