我正在尝试重新创建矢量类以供我自己理解。我最近在尝试编译此代码时遇到了 Visual Studio 2013 的问题。对于每个“向量”关键字,我都收到一个错误消息:
我不知道这是我的类定义中的范围错误还是其他原因。我没有玩过任何设置。
#include "../../../std_lib_facilities.h"
class vector{
int sz;
double* elem; //pointer to the first element (of type double)
public:
vector(int s) :sz(s), //constructor - allocates s doubles , :size(s) is a 'initilization list'
elem(new double[s])
{
for (int i = 0; i < s; i++)
elem[i] = 0; //initialize elements
}
int size() const
{
return sz;
}
//read
double get(int n)
{
return elem[n];
}
//write
void set(int n, double v)
{
elem[n] = v;
}
//Every class that owns a resource needs a destructor
~vector() //destructor
{
delete[] elem; // free memory
}
};
int main(int argc)
{
vector v(5);
for (int i = 0; i < v.size(); i++){
v.set(i, i);
cout << "v[" << i << "]==" << v.get(i) << '\n';
}
system("PAUSE");
}
如果您需要更多信息,请随时询问。