【发布时间】:2013-04-19 18:06:30
【问题描述】:
操作系统:xp
IDE:VS 2008
在我用 Visual C++ 做的项目中,我已经在托管类中声明了一个 std::vector
std::vector<pts> dataPoints;//this gives error c4368 : mixed type not allowed
但是这行得通
std::vector<pts> * dataPoints;//a pointer to the vector
然后我在托管类的构造函数中在免费商店中创建了这个向量
dataPoints = new std::vector<pts>(noOfElements,pts());//which is not so attractive.
我需要向量的原因是因为我正在通过ifstream 读取文件并将这些值存储在向量中。
Q1)为什么我能够声明一个指向本机类型对象的指针(我猜)但不是一个对象?
此外,在尝试向量之前,我尝试了托管数组
cli::array<Point> dataPoints //and i defined it later.
但是当我这样做时
ifile >> dataPoints[i].X;
它给出了一个错误 c2678 : operator= 没有为int 重载!!。
Q2)为什么我不能在这里使用托管代码。起初我认为它可能是一个包装类 Int 但随后自动拆箱(转换运算符)应该处理它?还是 Point::X 符合property 的条件,因此不被认为是正常的int?我错过了什么?
这就是我选择vector 和pts 解决方案的原因。pts 如下
struct pts
{
int X, int Y;
pts() : X(0),Y(0){}
pts(int x,int y) : X(x),Y(y){}
};//this i created to store the data from the file.
【问题讨论】:
标签: c++ visual-c++ stl managed-c++