问题描述
我不断收到非静态成员引用必须相对于特定对象"错误而且我实际上不明白我的代码仅包含2个类:1-信息2名员工
I Keep getting the 'nonstatic member reference must be relative to specific object' errorand i don't actually get it my code is simply Consists of 2 classes :1-Info2-Employee
和Employee类从Info继承
and Employee class inherit from Info
INFO类具有2个变量:ID-名称.并且我正在尝试像这样访问ID变量
the INFO class has 2 variable : ID - Name.and I'm trying to access the ID variable like this
cin >> Employee.Info.ID
有什么帮助吗?
推荐答案
错误消息明确指出,您正在尝试访问不带对象的对象成员:您需要区分类和对象. class
定义该特定 class
的对象的外观,即对象支持哪些数据成员和函数成员.您可能想按照以下方式做一些事情
The error message clearly states that you are trying to access an object's member without an object: You need to distinguish classes and objects. A class
defines how objects of that particular class
look like, i.e., what data members and function members the objects support. You probably want to do something along the lines of
Employee worker;
worker.Info.ID = "ID";
std::cout << worker.Info.ID << '\n';
(假设 Info
恰好是 Employee
的数据成员,而其类型又具有 ID
数据成员.
(assuming Info
happens to be a data member of Employee
whose type in turn has an ID
data member.
这篇关于无法理解错误消息:“非静态成员引用必须相对于特定对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!