gpt4 book ai didi

c++ - C++中如何访问类的私有(private)数据成员

转载 作者:行者123 更新时间:2023-11-28 01:33:28 25 4
gpt4 key购买 nike

以下代码在我的“struct node* createNode”函数行 30 和 31 中显示了两个错误。错误是未声明“temp”和不完整类型“struct node”的无效使用。如何解决这个问题呢??

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

最佳答案

从公共(public)成员函数返回私有(private)类型是很奇怪的但这是如何完成的

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

关于c++ - C++中如何访问类的私有(private)数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50508767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com