类型 myObj 和 myObj* 不兼容

Types myObj and myObj* are not compatible

本文关键字:myObj 不兼容 类型      更新时间:2023-10-16
class boundaryPt{
public:
friend class KCurvature;
int x;
int y;
boundaryPt(int x, int y){
    this->x = x;
    this->y = y;
}
boundaryPt(){}
};

class KCurvature{
public:
    boundaryPt* boundaryPtAry;
    int numPts;
    ifstream input;
KCurvature(char* inFile){
    input.open(inFile);
    input >> numPts;
    boundaryPtAry = new boundaryPt[numPts];
}
void loadData(char* inFile){
    input.open(inFile);
    int x;
    int y;
    while(!input.eof()){
        input >> x;
        input >> y;
        boundaryPtAry[index++] = new boundaryPt(x,y);
    }
};

我的问题是:

boundaryPtAry[index++] = new boundaryPt(x,y);

我正在尝试将我的 boundaryPt 对象存储在我的 boundaryPt 类型数组中,但由于我将该数组声明为 boundaryPt*,因此不允许我存储 boundaryPt。

这是一个引用指针的简单问题吗?我对C++生疏了。

解决了!我现在意识到,在创建对象数组时,您不仅要创建数组,还要创建对象本身。因此,无需创建一个新对象并尝试将其放入数组中(或者在我的情况下将数组索引指向它)。

while(!input.eof()){
    input >> boundaryPtAry[index].x;
    input >> boundaryPtAry[index].y;
    index++;
}