2016-04-27 12 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); 
    } 
}; 

Mein Problem ist, mit:Typen myObj und myObj * sind nicht kompatibel

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

Ich versuche, meine boundaryPt Objekte in meinem Array vom Typ boundaryPt zu speichern, aber da ich erklärt, dass Array als boundaryPt * es wird mir nicht erlauben, ein boundaryPt zu speichern.

Ist das ein einfaches Problem, einen Zeiger zu deaktivieren? Ich bin rostig mit C++.

+1

Wie wäre es mit 'boundaryPtAry [index] .x = x; boundaryPtAry [index] .y = y; index ++; '? – songyuanyao

+0

Das hat funktioniert. Ich erkenne jetzt, dass wenn ich ein Array von Objekten erstelle, es tatsächlich die tatsächlichen Objekte erzeugt. Es ist also nicht notwendig, ein neues boundaryPt-Objekt zu erstellen. Vielen Dank! – user5904091

+1

10 @ user5904091 Bitte posten Sie Ihre Antwort als Antwort, anstatt die Frage selbst zu aktualisieren. –

Antwort

0

Gelöst! Mir ist jetzt klar, dass Sie beim Erstellen eines Arrays von Objekten nicht nur ein Array erstellen, sondern auch die Objekte selbst. Es war also nicht notwendig, ein neues Objekt zu erstellen und es in das Array einzufügen (oder in meinem Fall sollte der Array-Index darauf zeigen).

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

kommst du von einem Java-Hintergrund? –

+0

Kurze Antwort: ja. Lange Antwort: Ich bin ein Student und fast alle meine Kurse haben Projekte in Java bis zu diesem Semester zugewiesen, so habe ich viel weniger Erfahrung mit C++ als Java. Aber ich bin froh, dass ich heute etwas Neues über Arrays von Objekten gelernt habe. – user5904091

Verwandte Themen