【问题标题】:expression preceding parentheses of apparent call must have (pointer-to-) function type明显调用括号前的表达式必须具有(指向)函数类型
【发布时间】:2017-03-05 03:05:26
【问题描述】:

我在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;
}

当我构建这个程序时,我在最后一行得到错误。它说:

明显调用括号前的表达式必须具有(指向)函数类型

我该怎么办?

【问题讨论】:

  • 你有没有机会包括Windows.h

标签: c++ c++11


【解决方案1】:

对于其他任何人,这也可能是因为重新定义了方法或属性名称。即属性和方法可能具有相同的名称

【讨论】:

  • 大加一。谢谢。
  • 或者只是在属性名称后使用()
  • 哦,伙计,你可能刚刚为我节省了一个小时的时间。
【解决方案2】:

该错误表明试图调用未定义为函数的函数 max()。将 const 关键字后的括号更改为标识符 max 后的括号:

T max const()...

T max() const ...

【讨论】:

    【解决方案3】:
    • 添加所需的标头包含和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;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      相关资源
      最近更新 更多