i want to in main to call a class inside other class in c++, but i couldnt do it

Related searches
class linkedlist{
     class listNode{
          int data;
          listNode* next;
            void listNode(int data,listNode* next){
            this->data=data;
            this->next=next;
        }
};

int main(){
  // in here I want to creat a listnode, but i dont know how to do it

  insert(listNode,5);
}

I need to call the listnode in main() function, I know I can use linkedlist list; and then list.listNode, but I need a node in order to fill in the insert() function.

You can simply create a listNode variable like below

int main(){
linkedlist::listNode aNode(5, NULL);
// or
linkedlist::listNode *pNode = new linkenlist::listNode(5,NULL);
....

}

Initialising classes inside another class in C++?, Basically, how it works is thus: If the Child class is fully defined before the Owner class, it can be included in the Owner in it's entirety, or as a� At the end of the pivot operations, I generated output by calling the header object and then each row object in turn to generate its output back to the main class. Separate classes weren't appropriate because A) the nested classes did need some data from the master class and B) the processing was very specific and not useful elsewhere.

listNode is a class defined inside linkedList class. This means that you need to enter appropriate namespace before usage of described symbol. Also, you need to call constructor of your desired class. To be honest, you better should read some C++ tutorial, because your example has more errors.

Valid very simple implementation would be:

#include <iostream>

class linkedList {
public:
    class listNode {
    public:
        int data;
        listNode* next;

        listNode(int data_) : data(data_), next(nullptr) {}
    };

    linkedList() : head(nullptr), tail(nullptr) {}

    void insert(listNode* new_node) {
        if (head == tail && tail == nullptr) {
            head = tail = new_node;
        }
        else {
            tail->next = new_node;
            tail = new_node;
        }
    }

    listNode* getFront() { return head; };

    listNode* getBack() { return tail; };

    listNode* head;
    listNode* tail;
};

int main(){
    linkedList list;
    list.insert(new linkedList::listNode(5));
    list.insert(new linkedList::listNode(6));
    list.insert(new linkedList::listNode(7));

    std::cout << list.getFront()->data << std::endl; // 5
    std::cout << list.getBack()->data << std::endl;  // 7
    return 0;
}

Nested Classes in C++, A nested class is a class that is declared in another class. of the enclosing class have no special access to the members of a nested class. cout<<"The number is "<<num; } }; }; int main() { cout<<"Nested classes in C++"<<� This is how my class mcCalculator looks. Its has five functions besides constructor and destructor, called Add, Subtract, Devide (that was a spelling mistake), Multiply, and DisplayOutVal. As you can see from their names, these functions add, subtract, multiply, and divide and store the results in a private variable iOutVal which is called by

enter image description here

enter code here
class linkedlist{
public:
class listNode{
public:
        int data;
        listNode* next;
        listNode(int data,listNode* next){
            this->data=data;
            this->next=next;
        }
        void printNode(listNode* node,ofstream& outFile){
            outFile<<"( "<<node->data<<","<<node<<","<<node->next<<","<<node->next- 
 >data<<")"<<endl;
        }
};
//  listNode* listHead;
//  listNode* listHead(){
//      return new listNode(-9999,NULL);
//  }
listNode* findSpot(listNode* listHead,listNode* newNode){
    listNode* spot=listHead;
    while(spot->next!=NULL && spot->next->data<newNode->data)
        spot=spot->next;
    return spot;
}
void listInsert(listNode* listHead,listNode* newNode){
    listNode* spot=findSpot(listHead,newNode);
    newNode->next=spot->next;
    spot->next=newNode;
}
void printList(listNode* listHead,ofstream& outFile){
    listNode* c=listHead;
    outFile<<"listHead -> ";
    while(c->next!=NULL){
        outFile<<"( "<<c->data<<","<<c<<","<<c->next<<","<<c->next->data<<") -> ";
        c=c->next;
    }
    outFile<<" NULL "<<endl;
}
void constructLL(linkedlist::listNode* listHead,ifstream& inFile,ofstream& outFile2){
    int a;
    cout<<listHead->data<<endl;
    while(inFile>>a){
        listNode* newNode=new listNode(a,NULL);
        listInsert(listHead,newNode);
        printList(listHead,outFile2);
    }
}
listNode* findMiddleNode(listNode* listHead,ofstream& outFile2){
    listNode* walk1=listHead;
    listNode* walk2=listHead;
    listHead->printNode(walk1, outFile2);
    while(walk2!=NULL && walk2->next!=NULL){
        walk1=walk1->next;
        walk2=walk2->next->next;
    }
    return walk1;
}
};
int main(int argc ,char* argv[]) {
   ifstream inFile(argv[1]);
   ofstream outFile1(argv[2]);
    ofstream outFile2(argv[3]);
//  linkedlist list;
//  list.listHead();
cout<<"123";
linkedlist:: listNode listHead(-9999,NULL);
//  list.sethead(-9999);
//  listHead=new listNode(-9999,NULL);
 //     cout<<list.gethead();
constructLL(listHead,inFile,outFile2);
//  printList(listHead,outFile1);
//  listNode* middleNode=findMiddleNode(listHead,outFile2);
//  listHead->printNode(middleNode,outFile1);
inFile.close();
outFile1.close();
outFile2.close();
return 0;
}

How to call a class from another class in Java, As Java Class , we can have classes as part of another class. i.e. we can have This way of including classes inside other classes is also referred as class composition or has a relation. . There is no Steven Abell, Java, Smalltalk, Lisp, C, C++, multiple assembly languages class A{. public static void main(String args[]) {. 3. Create constructor in your project’s main class. For example my default class name is MyProject, so i am creating the constructor inside MyProject class. Inside this constructor we would create object of Second class named as Obj. Object is used to call the function from another class.

Calling a Class From Another Class in Java, How to Call a Class Inside Another Class In Java. have created an object of Fan class, at LINE C and LINE D we have created two objects of Light class - one Also change the code in the main method to use the newly created constructor. C++ class and functions: Inside the class definition As the name suggests, here the functions are defined inside the class. Functions defined inside the class are treated as inline functions automatically if the function definition doesn’t contain looping statements or complex multiple line operations.

Nested classes (C++ only), A nested class is declared within the scope of another class. cannot allow the following // statement because C::y is private: // int z = p->y; } }; int main() { } Thanks for the info. In my main.cpp file, I create an instance of class A (call it A_instance). When I create A_instance, I don't need to call f in main.cpp for what I'm trying to do here (but I do need to call f in main for other parts of my main.cpp file). (So, I don't think I can make f static.)

Namespace is a feature added in C++ and not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.

Comments
  • Please make a minimal reproducible example, including a detailed description of misbehaviour, full verbatim quotes of any error or warning you might get (as text).
  • "i know i can use linkedlist list; and then list.listNode" Did you try it? If you did, you would know, that it wouldn't work either.
  • The nested class is part of the outer class so you need a bit more scope resolution, linkedlist::listNode, to correctly identify the nested class. You also need to sort out the naming of the listNode function. Is it supposed to be a constructor? And when you call it, you need to provide parameters. I strongly recommend consulting a good set of reference materials. C++ is not a good language to try to learn by Internet or trial and error.
  • i was asked by my constructor doing like this. a linkedlist class {and a listnode class and a constructLL(..)} , and int the main function i need to creat a listhead, and constructLL(listhead)
  • thanks, that help me a lot, but I have one thing left, i use "linkedlist:: listNode listHead(5,null) in main class, and now ,i want to insert(listHead,10), so what I wrote is void insert(listNode* listHead,int n), but it says insert was not declared, I know now listNode* listHead might be wrong, but i dont know how to fix it
  • You can define insert function like void insert(linkedlist::listNode *listHead, int n) { // find last node of list linkedlist::listNode *lastNode = listHead; while (lastNode->next != null) lastNode = lastNode->next; // append new node to end of list lastNode->next = new linkedlist:: listNode(n, NULL); }
  • i tried this way, it still says insert was not declared, the insert function is inside the linkedlist class
  • You can move it to out of class than you can use it. The answer given by @jul3x is more convenient and complete solution.
  • well, I cant. My professor required sheet said the insert has to be inside linkedlist class
  • list.insert(linkedlist::listNode(5)); won't compile because insert() is expecting a listNode* pointer but you are giving it a listNode instance instead. You would need to use list.insert(new linkedlist::listNode(5)); , but now you have to deal with proper dynamic memory management, the Rule of 3/5/0, etc. insert() should take just an int as input and create a new node for it, the caller should not be creating nodes directly
  • Yeah, I just modified code. But for memory management - if list is created like this - it does not matter - let the OS take care of cleaning up.
  • well, thanks for correct me, but the point i confused is my instructor ask me to get something like this. A linkedlist class { a listNode class { data,next} and constructLL(listNode* listHead)} which here is ok for me, but in main function, she ask me,get a new listnode which is listHead,and using the function constructLL(listHead); which is confused me