MSVC ++编译器错误C2143 [英] MSVC++ compiler error C2143
问题描述
以下代码摘录负责隐藏的MSVC ++编译器错误:
template< class T&类Vec:public vector< T> {
public:
Vec():vector< T>(){}
Vec(int s):vector&
T& operator [](int i){return at(i); }
const T&运算符[](int i)const {return at(i);}
};
...
错误:
test.cpp(5):错误C2143:语法错误:缺少','before'<'
test.cpp(12):参见类模板实例化Vec T被编译
如何解决这个问题? / p>
---编辑---
某些上下文:
我试图编译基本上从 C ++编程语言复制和粘贴的代码。我还不完全了解这个代码。但是,目的是实现一个向量类型,当某些代码试图访问向量范围中的一个项而不是返回不正确的值时,它会抛出异常。
尝试
模板< class T& class Vec:public vector< T> {
public:
Vec():vector(){} // no<
Vec(int s):vector(s){} //相同
T& operator [](int i){return at(i); }
const T&运算符[](int i)const {return at(i);}
};
模板类的构造函数在其名称中不包含模板签名。
注意,你的第二个构造函数应该是
Vec(typename vector< T> :: size_type s):vector(s){} //不一定int
<你真的不应该从向量派生,因为它有一个非虚拟析构函数。不要尝试通过指向向量的指针删除Vec。
The following code excerpt is responsible for a cryptic MSVC++ compiler error:
template<class T> class Vec : public vector<T>{
public:
Vec() : vector<T>(){}
Vec(int s) : vector<T>(s){}
T& operator[](int i){return at(i); }
const T& operator[](int i)const{ return at(i);}
};
...
The error:
test.cpp(5) : error C2143: syntax error : missing ',' before '<'
test.cpp(12) : see reference to class template instantiation 'Vec<T>' being compiled
How do I fix this?
---Edit---
Some context:
I am trying to compile code essentially copy and pasted from The C++ Programming Language. I don't yet even understand this code completely. The purpose, however, is to implement a vector type that will throw an exception when some code attempts to access an item out of the vector's range instead of just returning incorrect values.
Try
template<class T> class Vec : public vector<T>{
public:
Vec() : vector(){} // no <T>
Vec(int s) : vector(s){} // same
T& operator[](int i){return at(i); }
const T& operator[](int i)const{ return at(i);}
};
The constructor for a template class does not include the template signature in its name.
As a side note, your second constructor should really be
Vec(typename vector<T>::size_type s) : vector(s){} // not necessarily int
Finally, you really shouldn't derive from vector, as it has a non-virtual destructor. Do not attempt to delete a Vec through a pointer to a vector.
这篇关于MSVC ++编译器错误C2143的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!