c++ - 是什么导致 C++ 编译器错误 : must have argument of class or enumerated type?

标签 c++ compiler-errors

函数声明:


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/

相关文章:

c++ - 使用 fscanf 设置不同类中的变量

c++ - 在插入期间检测周期

c# - 使用C#列表

覆盖从函数内声明的 NSObject 继承的子类的函数时出现 Swift 编译器错误

c++ - 编译器是否假定 "this"在 Debug模式下不是 nullptr?

c++ - 在 C++ 中将小数与变量相乘时出错

c++ - boost T_CONTLINE token 不起作用

c++ - GCC 4.7 从初始化器列表初始化 unique_ptrs 容器失败

java - 引用一个在编译时无法解析的变量。 (java)

java - Android findViewById()、getComponentName() 和 getSystemService() : Cannot make a static reference to a nonstatic field from type Activity