【问题标题】:Error while calling protected function of base class调用基类的受保护函数时出错
【发布时间】:2016-03-26 22:25:46
【问题描述】:

我是 C++ 的初学者。我正在研究继承。在我的代码中,当我尝试从派生类调用基类的成员函数时,我收到错误statement cannot resolve address of overloaded function。我已经应用了范围运算符并且基类的成员函数受到保护,因此派生访问函数没有问题。我在这里做错了什么?这是我的代码:

#include <iostream>
#include <string.h>

using namespace std;
class Employee
{
private:
    char *emp_name;
    int emp_code;
    char *designation;

protected:
    Employee(char *name ="",int code = 0,char *des = "", int nlength=1, int dlength=1): emp_code(code)
    {
        emp_name = new char[nlength+1];
        strncpy(emp_name,name,nlength);

        designation = new char[dlength+1];
        strncpy(designation,des,nlength);
    }

    ~Employee()
    {
        delete[] emp_name;
        delete[] designation;
    }

    char* GetName()
    {
        return emp_name;
    }

    int GetCode()
    {
        return emp_code;
    }

    char* GetDes()
    {
        return designation;
    }
};

class Work: public Employee
{
private:
    int age;
    int year_exp;

public:
    Work(char *name ="", int code = 0, char *des = "", int nlength=1, int dlength=1, int w_age=0, int w_exp=0):Employee(name,code,des,nlength,dlength),age(w_age),year_exp(w_exp)
    {
    }

    void GetName()
    {
        Employee::GetName;
    }

    void GetCode()
    {
        Employee::GetCode;
    }

    void GetDes()
    {
        Employee::GetDes;
    }

    int GetAge()
    {
        return age;
    }

    int GetExp()
    {
        return year_exp;
    }

};

int main()
{
    using namespace std;
    Work e1("Kiran",600160,"Implementation Specialist", strlen("Kiran"),strlen("Implementation Specialist"),24,5);

    cout << "Name: " << e1.GetName() << endl;
    cout << "Code: " << e1.GetCode() << endl;
    cout << "Designation: " << e1.GetDes() << endl;
    cout << "Age: " << e1.GetAge() << endl;
    cout << "Experience: " << e1.GetExp() << endl;
}

另外,我收到错误 no-match for operator&lt;&lt;。我没有打印任何课程。我只是在这里调用派生类的函数。

【问题讨论】:

  • Employee::GetCode 不调用任何东西。 Employee::GetCode() 会。
  • e1.GetName() 不返回任何值(Work::GetName 的返回类型为void),因此不清楚您要打印的是什么。

标签: c++ function class inheritance member-functions


【解决方案1】:

如前所述,派生类Work::GetNamevoid,因此它不返回任何值。您可以将其更改为:

class Work: public Employee
{
//...
public:
//...
    char *GetName()
    {
        return Employee::GetName();
    }

或者,如果您只想重用基类Employee::GetName(),但在派生类中将其公开,您只需在Work 中将其重新声明为公开即可。

class Work: public Employee
{
//...
public:
//...
    using Employee::GetName; // <--- this re-declares the inherited GetName() as public


[编辑] 更改了“重新声明”代码以遵循当前的 C++ 最佳实践,感谢@AlanStokes 的评论。具体来说,“访问声明”就像最初使用的那样
    Employee::GetName; // <--- this re-declares the inherited GetName() as public

自 C++98 以来已被弃用,取而代之的是具有相同效果的“使用声明”。

    using Employee::GetName; // <--- this re-declares the inherited GetName() as public

【讨论】:

  • 现代语法是using Employee::GetName;。没有using 的版本称为访问声明,已被弃用近20年。
【解决方案2】:

C++ 确实不允许 允许引用只是命名它们的成员。你要么需要像&amp;Employee::GetDes 这样的地址,要么像Employee::GetDes() 这样打电话给他们。您可能还想确保您实际上正在返回结果,但仅调用成员就可以工作,尽管它不太可能产生所需的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2019-08-27
    • 2014-11-03
    • 2017-02-22
    相关资源
    最近更新 更多