调用基 class 的受保护函数时出错
Error while calling protected function of base class
我是 C++ 的初学者。我正在研究继承。在我的代码中,当我尝试从派生 class 调用基 class 的成员函数时,出现错误 statement cannot resolve address of overloaded function
。我应用了作用域运算符,并且基 class 的成员函数受到保护,因此派生函数访问这些函数没有问题。我在这里做错了什么?这是我的代码:
#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<<
。我没有打印任何 class 。我只是在这里调用派生的 class 的函数。
C++ 不 允许引用成员只是命名它们。您要么需要像 &Employee::GetDes
这样的地址,要么需要像 Employee::GetDes()
这样称呼他们。您可能还想确保您确实返回了结果,但仅调用该成员就可以了,尽管它不太可能产生所需的结果。
如前所述,派生的 class Work::GetName
是 void
,因此它不返回任何值。您可以将其更改为:
class Work: public Employee
{
//...
public:
//...
char *GetName()
{
return Employee::GetName();
}
或者,如果您只想重用基础 class Employee::GetName()
但在派生 class 中将其设为 public,您需要做的就是重新在 Work
.
中声明为 public
class Work: public Employee
{
//...
public:
//...
using Employee::GetName; // <--- this re-declares the inherited GetName() as public
[编辑] 更改了 "re-declare" 代码以遵循当前的 C++ 最佳实践,感谢@AlanStokes 的评论。具体来说,"access declarations"像原来用的那个
Employee::GetName; // <--- this re-declares the inherited GetName() as public
自 C++98 以来已弃用,取而代之的是具有相同效果的 "using declarations"。
using Employee::GetName; // <--- this re-declares the inherited GetName() as public
我是 C++ 的初学者。我正在研究继承。在我的代码中,当我尝试从派生 class 调用基 class 的成员函数时,出现错误 statement cannot resolve address of overloaded function
。我应用了作用域运算符,并且基 class 的成员函数受到保护,因此派生函数访问这些函数没有问题。我在这里做错了什么?这是我的代码:
#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<<
。我没有打印任何 class 。我只是在这里调用派生的 class 的函数。
C++ 不 允许引用成员只是命名它们。您要么需要像 &Employee::GetDes
这样的地址,要么需要像 Employee::GetDes()
这样称呼他们。您可能还想确保您确实返回了结果,但仅调用该成员就可以了,尽管它不太可能产生所需的结果。
如前所述,派生的 class Work::GetName
是 void
,因此它不返回任何值。您可以将其更改为:
class Work: public Employee
{
//...
public:
//...
char *GetName()
{
return Employee::GetName();
}
或者,如果您只想重用基础 class Employee::GetName()
但在派生 class 中将其设为 public,您需要做的就是重新在 Work
.
class Work: public Employee
{
//...
public:
//...
using Employee::GetName; // <--- this re-declares the inherited GetName() as public
[编辑] 更改了 "re-declare" 代码以遵循当前的 C++ 最佳实践,感谢@AlanStokes 的评论。具体来说,"access declarations"像原来用的那个
Employee::GetName; // <--- this re-declares the inherited GetName() as public
自 C++98 以来已弃用,取而代之的是具有相同效果的 "using declarations"。
using Employee::GetName; // <--- this re-declares the inherited GetName() as public