【发布时间】:2022-01-17 02:16:56
【问题描述】:
我正在尝试创建一个公共成员 get 函数,该函数将返回私有变量 emp。但是我的 get 函数中出现错误提示
“返回值类型与函数类型不匹配”
我知道在 C++ 中,返回类型不能是数组,而必须是指向数组的指针。但我只是返回数组的名称,它应该是第一个元素的地址。
class DB {
private:
static const int SIZE = 13;
Employee* emp[SIZE];
public:
DB();
void print(Employee*[], const int);
Employee* get_emp_arr();
const int get_emp_arr_size();
}
Employee* DB::get_emp_arr(){
return this->emp;
}
我想出了解决我的错误消息的方法,但我想要一个解释。只需将返回类型更改为“Employee**”,“this->emp”就被接受为返回类型。
【问题讨论】:
-
也许你的意思是
Employee emp[SIZE]; -
除了@Damien 的评论:
const Employee* get_emp_arr();与返回非constEmployee*的实现不匹配 -
如错误所示,“返回值
Employee*与函数类型const Employee* ...不匹配” -
我的错误,const Employee* 是一个错字。我删除了它,但我的问题是一样的。
-
使用新编辑的代码,您将返回
Employee*[13]类型的对象,但您将返回类型声明为Employee*。你看到“一个 13 个指向 Employee 的指针的数组”不是“一个指向 Employee 的指针”吗?
标签: c++ class getter-setter encapsulation return-type