I need to display an array of structures, as far as I understand, I need to use a memory shift, having a pointer to the first element of the array. When trying to fix the code, we saw the following error:
Invalid types ‘[int]’ for array subscript
my code:
#include <iostream>
#include <string.h>
using namespace std;
typedef struct Point3D{
double x;
double y;
double z;
}Point3D;
Point3D createPoint3D(double xnum, double ynum, double znum){//функція створення точки
Point3D point;
point.x = xnum;
point.y = ynum;
point.z = znum;
return point;
}
void PrintPoint(Point3D point){//використовується в інщій ф-ції, по суті внутрішня не основна функція
cout << "(" << point.x << ","<< endl;
cout << point.y<< ","<< endl;
cout << point.z<< ";"<< "\n"<< endl;
}
class Obj3D{
private:
Point3D *array_of_points;
int number_of_points;
public:
void setAddPoint(Point3D point){
*(array_of_points sizeof(point)*number_of_points) = point;
number_of_points ;}
int getNumber(){
return number_of_points;}
Point3D *getArray(){
return array_of_points;}
Obj3D(Point3D point, int number){
number--;
*(array_of_points number) = point;}
Obj3D(){
*array_of_points = createPoint3D(0.0, 0.0, 0.0);
number_of_points = 1;
}
~Obj3D(){}
};
void printObj(Obj3D Obj){
for(int x = 0; x<=Obj.getNumber(); x ){
PrintPoint(Obj.getArray()[x]);
}
}
int main(int argc, char* argv[]){
Obj3D Obj;
Point3D point1 = createPoint3D(-0.5,-0.5,-0.5);
Point3D point2 = createPoint3D(0.5,-0.5,-0.5);
Point3D point3 = createPoint3D(-0.5,-0.5,0.5);
Point3D point4 = createPoint3D(0.5,-0.5,0.5);
Point3D point5 = createPoint3D(0.0,0.5,0.0);
Obj.setAddPoint(point1);
Obj.setAddPoint(point2);
Obj.setAddPoint(point3);
Obj.setAddPoint(point4);
Obj.setAddPoint(point5);
cout<<Obj.getArray();
exit(0);
}
You can not watch the main function, it is most likely not correct, I want to deal with the functions of the output and set
CodePudding user response:
You absolutely don't need to memory shift
*(array_of_points sizeof(point)*number_of_points) = point;
C is not that complicated, just
array_of_points[number_of_points] = point;
Because the compiler knows the type involved, Point3D
, it's quite capable of working out the memory shift by itself. Also by the rules of C *(p n)
is exactly equivalent to p[n]
but generally prefer the latter because it is easier to read.
Other errors (which you'll find out when you run the code). Class Obj3D
has an uninitialised pointer array_of_points
. You use the pointer, e.g. here
Obj3D(){
*array_of_points = createPoint3D(0.0, 0.0, 0.0);
number_of_points = 1;
}
but you never give the pointer a value. So that is going to crash your program.
You need to allocate memory using new
for your pointer to point at. That doesn't happen automatically. E,g,
Obj3D() {
array_of_points = new Point3D[100]; // allocate 100 points
array_of_points[0] = createPoint3D(0.0, 0.0, 0.0);
number_of_points = 1;
}
CodePudding user response:
The problem(besides the typo of missing )
) is that you're not calling method getArray
and instead using operator[]
.
To solve this call the method getArray
by replacing Obj.getArray[x]
with Obj.getArray()[x]
as shown below:
//---------------------vv---v---->parenthesis added for calling and also for closing
PrintPoint(Obj.getArray()[x]);