-3
    //This is class for Linked list 
    class SinglyLinkedListNode {
        public:
            int data;
            SinglyLinkedListNode *next;

            SinglyLinkedListNode(int node_data) {
                this->data = node_data;
                this->next = nullptr;
            }
    };



      SinglyLinkedListNode*    insertNodeAtHead(SinglyLinkedListNode* head, int key  )
        {
            SinglyLinkedListNode* newNode = &SinglyLinkedListNode(key);


           /*1st way of creating a node sing a class constructor 

            error(i got for this) :error: taking address of temporary [-fpermissive]
           */






           SinglyLinkedListNode* newNode =  new(SinglyLinkedListNode );//2nd way of creating a node 
            newNode->data=key;
            newNode->next=NULL;//following error i got by second method


 /*solution.cc:59:66: error: no matching function for call to ‘SinglyLinkedListNode::SinglyLinkedListNode()’
        SinglyLinkedListNode* newNode =  new(SinglyLinkedListNode );//2nd way of creating a node
                                                                  ^
solution.cc:10:9: note: candidate: ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’
         SinglyLinkedListNode(int node_data) {
         ^~~~~~~~~~~~~~~~~~~~
solution.cc:10:9: note:   candidate expects 1 argument, 0 provided
solution.cc:5:7: note: candidate: ‘constexpr SinglyLinkedListNode::SinglyLinkedListNode(const SinglyLinkedListNode&)’
  */

    }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    "error: no matching function for call to ‘SinglyLinkedListNode::SinglyLinkedListNode()" should be pretty clear: You default-construct an instance, and the class doesn't have a default constructor. – Some programmer dude Jan 15 '19 at 14:24
  • 1
    If you're that new that you don't understand the concept of default-constructors and construction, then you need to take a few steps back. Please [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Jan 15 '19 at 15:03
  • thanks man!! done For others who may have same doubt which i had in 1)struct we write Node* newNode = new(Node); but in class we need to write Node* newNode = new(Node(constructor_argument_ifany)); :) – Bharath Choudhary Jan 15 '19 at 18:12

1 Answers1

0

When you write new(SinglyLinkedListNode ); you are creating an object of type SinglyLinkedListNode on the heap and the compiler will try to call the default constructor for that object but the class doesn't have a default constructor. If you were to write new SinglyLinkedListNode( 6 ); the compiler would be able to call the only constructor you have, which takes an int parameter. My guess is that this worked as a struct rather than a class because the struct didn't have any CTORs and so the compiler was able to create a default CTOR for it.

Roy
  • 101
  • 4