开发者

not familiar with c++ pointer, need help

on the line *binSon[0].add(x);, it give me an error, an expression must have a class type, what do i do?

开发者_Go百科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);
        }
    }
};


You have a small precedence issue. Either of the following should fix the problem:

(*binSon[0]).add(x);

or

binSon[0]->add(x);

Same applies to the other line.


Your problem here really isn't quite what you think, although it can be solved as is.

The correct syntax is:

binSon[0]->add(x);

The -> operator is member-of-pointer, which handles everything for you. Typically, -> should be used with pointers and . with references or the object itself (the exceptions are in interesting spots).

To do it how you want (which you really shouldn't), (*binSon[0]).add(x) should work. The precedence of the operator is likely your issue at the moment.


Perhaps you want (*binSon[0]).add or better yet binSon[0]->add.


In C++, . has higher precedence than *, so when you write *binSon[1].add(x), it means *(binSon[1].add(x)), which isn't what you want. You can write (*binSon[1]).add(x), or better, binSon[1]->add(x), which is syntactic sugar for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜