Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression must have pointer to object type

Tags:

c++

matrix

I'm writing a matrix program and am currently trying to multiply a point and a matrix. I keep getting an error over my objects(result and P) "Expression must have pointer to 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 are:

    //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;
    }

};
like image 368
Nick Avatar asked Jul 13 '26 10:07

Nick


2 Answers

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

double element[4];

This is a one-dimensional array. However, in your function, you're 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 exactly how your matrix multiplication is supposed to work.

like image 197
Adam Maras Avatar answered Jul 15 '26 07:07

Adam Maras


result.element is a 1 dimensional array. You are using two indices with it. That will 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;

}
like image 30
David Nehme Avatar answered Jul 15 '26 09:07

David Nehme