表达在明显呼叫的括号前表达式必须具有(指针到 - )函数类型
expression preceding parentheses of apparent call must have (pointer-to-) function type
我正在学习VS2015社区上的C 模板。在这里是我的代码,我想定义一个模板类,并在main()
函数中调用成员函数。
template <typename T>
class Arithmetic {
T _a;
T _b;
Arithmetic() {};
public
Arithmetic(T a, T b) :_a(a), _b(b) {};
T max const() { return _a + _b; };
T minus const() { return _a - _b; };
};
int main() {
Arithmetic<int> ar(5,6);
cout << ar.max() << endl;
}
构建此程序时,我在最后一行会出现错误。它说:
表观呼叫的括号前表达式必须具有(指针到 - )函数类型
我该怎么办?
对于其他任何人来说,这也可能是由于重新定义方法或属性名称。即属性和方法可能具有相同的名称
错误指示尝试调用未定义为函数的函数max()。将const关键字之后的括号更改为标识符max之后:
T max const()...
to
T max() const ...
- 添加所需的标题包含和
using
- 添加
:
public
- 将
const
移至正确位置
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
class Arithmetic {
T _a;
T _b;
Arithmetic() {};
public:
Arithmetic(T a, T b) :_a(a), _b(b) {};
T max() const { return _a + _b; };
T minus() const { return _a - _b; };
};
int main() {
Arithmetic<int> ar(5,6);
cout << ar.max() << endl;
}
如果您将非恒定值发送到最大方法。
也可能引起此问题。这是最大的语法:
constexpr const T& max (const T& a, const T& b, Compare comp);
相关文章:
- C++中的双指针类型转换
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 将类指针类型转换为键时出错
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- C++在一个映射中存储不同的指针类型(并处理销毁)
- 指针类型类成员的动态强制转换的恒定性是什么?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- STL 函数和函数类型与函数指针类型
- 如何调用指针类型的方法(禁用多态性)?
- 为什么新表达式可以正确生成指针类型,即使它应该返回 void*?
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 是否允许调用方对我的 Builder 类使用任何指针类型(包括智能指针)?
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- 专门用于"direct"函数类型(与函数指针类型相对)
- 指向成员的指针类型和模板
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 在C++17中,为什么类模板和函数模板的指针类型推导明显不一致
- 为什么允许将整型、枚举和指向成员的指针类型reinterpret_cast到自身?