函数声明:
template <typename T>
Point<T>* operator +(Point<T> const * const point, Vector<T> const * const vector);
自从我使用 C++ 以来已经有一段时间了,所以也许我正在做一些非常愚蠢的事情。让我知道。
另外,不,我没有使用命名空间标准。
最佳答案
您在语言级别上做错的是为指针重载运算符。重载运算符的至少一个参数必须是用户定义的类型,或者是对一个参数的引用。
但是你在另一个层面上也做错了。您正在返回一个指针,这意味着您可能需要在运算符中动态分配一些存储空间。那么,谁拥有该存储空间?谁来发布?
您应该只获取引用并按值返回,例如:
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);
}
关于c++ - 是什么导致 C++ 编译器错误 : must have argument of class or enumerated type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493932/