如何在 c++ 中访问类的私有数据成员

How to access private data members of a class in c++

本文关键字:数据成员 访问 c++      更新时间:2023-10-16

以下代码在我的"struct node* createNode"函数第 30 行和第 31 行中显示两个错误。 错误是"临时"未声明和无效使用不完整类型的"结构节点"。如何解决这个问题??

#include <iostream>
using namespace std;
class myClass{
private:
struct node{
int data;
struct node *next;
};
struct node *head, *last, *temp;
public:
myClass();
bool isEmpty();
struct node *createNode();
void insertElement();
void deleteElement();
void displayList();
};
myClass::myClass(){
head=NULL;
last=NULL;
temp=NULL;
}
bool myClass::isEmpty(){
if(head==NULL)return true;
return false;
}
struct node *createNode(){
temp = new node;
return temp;
};
int main()
{
return 0;
}

从公共membre函数返回私有类型非常奇怪 但这是它是如何完成的

myClass::node *myClass::createNode(){ 
temp = new node;
return temp;
}

createNode(( 是 "myClass" 类的成员,将定义更改为:

node *myClass::createNode(){
temp = new node;
return temp;
}

并删除此函数后面的分号