The expression must have a pointer to the type of object

I am writing a matrix program, and currently I'm trying to multiply a point and a matrix. I keep getting an error on my objects (result and P). "Expression must have a pointer to an object type" in this function:

//Point Class functions Point Matrix44::operator*(const Point & P){ Point result; for (int i = 0; i < 4; i++) { for (int k = 0; k < 4; k++) { result.element[i][k] = 0; for (int j = 0; j < 4; j++) { result.element[i][k] = element[i][j] * P.element[j][k] + result.element[i][k]; } } } return result; } 

My two classes:

  //Matrix class class Point; class Matrix44 { private: double element[4][4]; public: Matrix44(void); Matrix44 transpose(void) const; friend istream& operator>>(istream& s, Matrix44& t); friend ostream& operator<<(ostream& s, const Matrix44& t); Matrix44 operator *(Matrix44 b); Point operator*(const Point & P); }; //Point class class Point { double element[4]; friend class Matrix44; public: Point(void) { element[0] = element[1] = element[2] = 0; element[3] = 1; } Point(double x, double y, double z){ element [0]=x; element [1]=y; element [2]=z; element [3]=1; } }; 
+4
source share
5 answers

In your Point class, you have an element member defined as:

 double element[4]; 

This is a one-dimensional array. However, in your function you are trying to access it, as if it were a two-dimensional array:

 result.element[i][k] P.element[j][k] 

I think you need to rethink how your matrix multiplication should work.

+13
source

result.element is a 1-dimensional array. You use two indexes with it. This does not compile. You should look at the definition of matrix multiplication .

 Point Matrix44::operator*(const Point & P){ Point result; for (int i = 0; i < 4; i++) { result.element[i] = 0; for (int j = 0; j < 4; j++) { result.element[i] += element[i][j] * P.element[j]; } } return result; } 
+2
source

Point::element is double[4] . In your code, you have Point result; result.element[i][k] = 0; Point result; result.element[i][k] = 0; . Since the element is not a two-dimensional array, the compiler tries to convert double to an array to use [] on it, but it cannot. I would suggest that this is the copied code from Matrix44 Matrix44::operator*(const Matrix44& M)

It always helps to tell us which line has the problem in your sample code.

In addition, the function will have the wrong result, you set result.element[i][k] to zero, and then set 4 different values. I think you wanted to add to the innermost loop instead of assignment.

+1
source

You are trying to access:

 result.element[i][k] = element[i][j] * P.element[j][k] + result.element[i][k]; 

When Point has only a single-level array:

 double element[4]; 
0
source

the same error may seem erroneous if you point to a structure that is not available to the source, for example

header.h

define struct myStruct;

source1.c

include header.h

 myStruct structX; 

source2.c

include header.h

 uint8_t * ptr2Struct = &structX; 

if this is the case, then you get this arrangement with the line "uint8_t * ptr2Struct = & structX;

0
source

All Articles