Mir Sazzat Hossain
Mir Sazzat Hossain

Reputation: 1

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

Following code is showing two error in my "struct node* createNode" function line 30 and 31. The errors are 'temp' was not declared and invalid use of incomplete type 'struct node'. How to solve this problem??

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

Upvotes: 0

Views: 119

Answers (2)

mrusom
mrusom

Reputation: 271

createNode() is a member of "myClass" class, change the definition to :

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

and remove that semicolon after this function

Upvotes: 0

Tyker
Tyker

Reputation: 3047

it is very strange to return a private type from a public membre fonction but here is how it is done

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

Upvotes: 2

Related Questions