表达在明显呼叫的括号前表达式必须具有(指针到 - )函数类型

expression preceding parentheses of apparent call must have (pointer-to-) function type

本文关键字:指针 类型 函数 表达式 呼叫      更新时间:2023-10-16

我正在学习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);
相关文章: