What causes C++ compiler error: must have argument of class or enumerated type?

前端 未结 3 1939
忘掉有多难
忘掉有多难 2020-12-20 14:38

Function declaration:


template 
Point* operator +(Point const * const point, Vector const * const vector);
         


        
3条回答
  •  臣服心动
    2020-12-20 15:07

    What you're doing wrong here on the language level is overloading operators for pointers. At least one argument of an overloaded operator must be of a user-defined type, or a reference to one.

    But you're also doing this wrong on another level. You're returning a pointer, which means you will probably need to allocate some storage dynamically in the operator. Well, who owns that storage? Who will release it?

    You should just take references and return by value, something like:

    template 
    Point operator +(Point const& point, Vector const& vector) {
        return Point(point.x + vector.x, point.y + vector.y);
    }
    

提交回复
热议问题