0

I am a beginner in C++. I am working on inheritance. In my code, when I try to call the member function of the base class from the derived class, i get the error statement cannot resolve address of overloaded function. I have applied the scope operator and the member function of the base class is protected, so that the derived do not have problem to access the functions. What am I doing wrong here? Here is my code:

#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;
}

Also, I get the error no-match for operator<<. I am not printing any class . I am just calling the function of the derived class here.

2
  • Employee::GetCode doesn't call anything. Employee::GetCode() does. Commented Dec 20, 2015 at 20:45
  • e1.GetName() doesn't return any value (the return type of Work::GetName is void), so it's not clear what it is you are trying to print. Commented Dec 20, 2015 at 20:46

2 Answers 2

1

As noted already, the derived class Work::GetName is void so it's not returning any value. You could change that to:

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

Or, if you just want to reuse the base class Employee::GetName() but make it public in the derived class, all you need to do is re-declare it as public in Work.

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


[EDIT] Changed the "re-declare" code to follow current C++ best practices, thanks @AlanStokes for the comment. Specifically, "access declarations" like the one originally used

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

have been deprecated since C++98 in favor of "using declarations" with the same effect.

    using Employee::GetName; // <--- this re-declares the inherited GetName() as public
Sign up to request clarification or add additional context in comments.

1 Comment

The modern syntax is using Employee::GetName;. The version without using is called an access declaration and has been deprecated for nearly 20 years.
0

C++ does not allow refering to members just naming them. You'll either need to take their address like &Employee::GetDes or you'll need to call them like Employee::GetDes(). You also might want to make sure you are actually returning the result but merely calling the member would work although it is unlikely to yield the desired result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.