简体   繁体   English

表达式必须具有指向对象类型的指针

[英]Expression must have pointer to object type

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: 我不断在我的对象上遇到错误(结果和P),此函数中“表达式必须具有指向对象类型的指针”:

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

};

In your Point class, you have the element member defined as: Point类中,您将element成员定义为:

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. 我认为您需要重新考虑矩阵乘法应该如何工作。

result.element is a 1 dimensional array. result.element是一维数组。 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;

}

Point::element is a double[4] . Point::elementdouble[4] In your code, you have Point result; result.element[i][k] = 0; 在代码中,您得到Point result; result.element[i][k] = 0; Point result; result.element[i][k] = 0; . Since element is not a two dimensional array, the compiler tries to convert the double to an array to use [] on it, but it can't. 由于element不是二维数组,因此编译器尝试将double转换为数组以在其上使用[] ,但不能这样做。 I would guess this is copy-pasted code from Matrix44 Matrix44::operator*(const Matrix44& M) 我想这是Matrix44 Matrix44::operator*(const Matrix44& M)复制粘贴的代码

It always helps to tell us what line has the problem in your sample code too. 告诉我们示例代码中哪一行也有问题总是有帮助的。

Also, the function will have the incorrect result, you set result.element[i][k] to zero, then set it to 4 different values. 另外,该函数将产生错误的结果,将result.element[i][k]设置为零,然后将其设置为4个不同的值。 I think you meant to add instead of assign in the innermost loop. 我认为您是要在最里面的循环中添加而不是分配。

same error might appear misleadingly if you point to a struct that is not accessible to the source, such as, 如果您指向源无法访问的结构,则可能会误导相同的错误,例如,

header.h 头文件

define struct myStruct; 定义结构myStruct;

source1.c 来源1.c

include header.h 包括header.h

myStruct structX;

source2.c source2.c

include header.h 包括header.h

uint8_t * ptr2Struct = &structX;

if that is a case then you get this arror with line " uint8_t * ptr2Struct = &structX; 如果是这种情况,则可以通过“ uint8_t * ptr2Struct =&structX;

You're trying to access: 您正在尝试访问:

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

When Point itself only have a one-level array: 当Point本身只有一个一级数组时:

double element[4];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM