*binSon[0].add(x);行上,它给我一个错误,一个表达式必须具有类类型,我该怎么办?

struct Rectangle
{
    tName Name; //name of the rectangle
    struct Rectangle *binSon[NDIR_1D]; //left and right son
    int Center[NDIR_1D];
    int Length[NDIR_1D];

    void add(Rectangle x){
        if(strcmp(x.Name,Name)<0)
        {
            if(binSon[0]==NULL)
                binSon[0]=&x;
            else
                *binSon[0].add(x);

        }else{
            if(binSon[1]==NULL)
                binSon[1]=&x;
            else
                *binSon[1].add(x);
        }
    }
};

最佳答案

尽管您可以按原样解决问题,但实际上您的问题并非您想的那样。

正确的语法是:

binSon[0]->add(x);


->运算符是指针成员,它将为您处理所有操作。通常,->应该与指针一起使用,而.与引用或对象本身一起使用(例外是在有趣的地方)。

要按照您想要的方式(实际上不应该这样做),(*binSon[0]).add(x)应该可以工作。目前,运营商的优先级可能是您的问题。

关于c++ - 不熟悉c++指针,需要帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7523553/

10-14 04:12