Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Function declaration:


template <typename T>
Point<T>* operator +(Point<T> const * const point, Vector<T> const * const vector);

It's been a while since I've used C++ so maybe I'm doing something really stupid. Let me know.

Also, no, I am not using namespace std.

like image 840
Anonymous Avatar asked Sep 21 '11 02:09

Anonymous


1 Answers

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 <typename T>
Point<T> operator +(Point<T> const& point, Vector<T> const& vector) {
    return Point<T>(point.x + vector.x, point.y + vector.y);
}
like image 63
R. Martinho Fernandes Avatar answered Sep 28 '22 11:09

R. Martinho Fernandes