2016-04-27 70 views
0
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); 
    } 
}; 

我的问题是:类型MyObj中和MyObj中*不兼容

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

我想保存在我喜欢的类型boundaryPt的阵列我boundaryPt对象,但自从我宣布数组boundaryPt *它不会让我存储一个边界点。

这是一个简单的问题,指出一个指针吗?我用C++生锈了。

+1

'boundaryPtAry [index] .x = x; boundaryPtAry [index] .y = y;索引++;'? – songyuanyao

+0

工作。我现在意识到,当我创建一个对象数组时,它实际上会创建实际的对象。所以它们甚至不需要创建一个新的boundaryPt对象。谢谢! – user5904091

+1

@ user5904091请将您的答案作为答案发布,而不是更新问题本身。 –

回答

0

已解决!我现在意识到,当创建一个对象数组时,你不仅仅是创建一个数组,而且也是对象本身。所以没有必要创建一个新的对象,并尝试将它放入数组(或者在我的情况下有数组索引指向它)。

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

你是从Java背景来的吗? –

+0

简答:是的。长的回答:我是一名学生,几乎所有的课程都在本学期之前用Java分配了项目,所以我对C++的经验比Java少。但我很高兴今天学到了一些关于物体阵列的新东西。 – user5904091