【问题标题】:Access specifier, not able to call a function which is defined in the derived class访问说明符,无法调用派生类中定义的函数
【发布时间】:2019-04-17 00:38:53
【问题描述】:

我试图了解c++中的访问说明符,所以我从基类中访问了一个公共和受保护的成员,并在派生类中执行了一个添加函数,如下所示,

#include<iostream>
using namespace std;

class Base
{
    public:
        int a;
    protected:
        int b;
};

class Derived : public Base
{
    public:
        int sum;
        void add()
        {   
            a = 10;
            b = 20;
            sum = a + b;
        }
};


int main()
{
    Derived Obj;
    Obj.add;
    cout << "The sum : " << Obj.sum << "\n";
    return 0;
}   

但在编译时我收到以下错误消息“语句无法解析重载函数的地址”谁能解释错误是什么?

提前致谢

【问题讨论】:

  • Obj.add; 应该是什么意思?这不是 C++ 中的有效语句。这就是编译器告诉你的。 C++ 中的函数通过() 运算符调用。如果你想调用一个函数,那么你的() 运算符在哪里?

标签: c++ access-specifier


【解决方案1】:

函数调用在以下行中缺少括号:

Obj.add;

应该是:Obj.add();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多