What causes C++ compiler error: must have argument of class or enumerated type?(什么导致 C++ 编译器错误:必须有类或枚举类型的参数?)
问题描述
函数声明:
template <typename T>
Point<T>* operator +(Point<T> const * const point, Vector<T> const * const vector);
我已经有一段时间没有使用 C++了,所以也许我正在做一些非常愚蠢的事情.让我知道.
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.
推荐答案
您在语言级别上做错的是重载指针运算符.重载运算符的至少一个参数必须是用户定义的类型,或者是对一个的引用.
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);
}
这篇关于什么导致 C++ 编译器错误:必须有类或枚举类型的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么导致 C++ 编译器错误:必须有类或枚举类型的参数?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
