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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7423424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:55:01  来源:igfitidea点击:

Expression must have pointer to object type

c++matrix

提问by Nick

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

};

回答by Adam Maras

In your Pointclass, you have the elementmember 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.

我认为您需要重新思考矩阵乘法应该如何工作。

回答by David Nehme

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.

result.element 是一维数组。您正在使用两个索引。那不会编译。您应该查看矩阵乘法的定义。

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;

}

回答by Mooing Duck

Point::elementis a double[4]. In your code, you have 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. I would guess this is copy-pasted code from Matrix44 Matrix44::operator*(const Matrix44& M)

Point::element是一个double[4]。在您的代码中,您有Point result; result.element[i][k] = 0;. 由于 element 不是二维数组,编译器尝试将 double 转换为数组以[]在其上使用,但它不能。我猜这是从复制粘贴的代码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. I think you meant to add instead of assign in the innermost loop.

此外,该函数将产生不正确的结果,您将其设置result.element[i][k]为零,然后将其设置为 4 个不同的值。我认为您打算在最内层循环中添加而不是分配。

回答by brain

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];

回答by Shoresh

same error might appear misleadingly if you point to a struct that is not accessible to the source, such as,

如果您指向源无法访问的结构,则可能会出现相同的错误,例如,

header.h

头文件.h

define struct myStruct;

定义结构 myStruct;

source1.c

源1.c

include header.h

包括 header.h

myStruct structX;

source2.c

源2.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;”行获得此错误。