4

I am trying to implement a linked list in C++ and don't know how to fix the error given below. I would greatly appreciate any help with debugging and, more importantly, an explanation of any underlying concepts that I don't understood regarding this error. Thanks.

Here is my error:

linkedList.cpp:19:23: error: no matching constructor for initialization of 'NodeType'
        newnode = new NodeType;
                      ^
./linkedList.h:10:9: note: candidate constructor not viable: requires single argument 'str', but no arguments
      were provided
        NodeType(const std::string str);
        ^
./linkedList.h:4:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1
      argument, but 0 were provided
class NodeType
      ^
linkedList.cpp:54:29: error: no matching constructor for initialization of 'NodeType'
    NodeType* newnode = new NodeType;
                            ^
./linkedList.h:10:9: note: candidate constructor not viable: requires single argument 'str', but no arguments
      were provided
        NodeType(const std::string str);
        ^
./linkedList.h:4:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1
      argument, but 0 were provided
class NodeType
      ^

Here is my NodeType class in my linkedList.h file:

  3 // define the node class
  4 class NodeType
  5 {
  6     private:
  7         std::string value;
  8         NodeType* next;
  9     public:
 10         NodeType(std::string str);
 11     friend class LinkedList;
 12 };

Here is the method in the linkedList.cpp file for the first error:

 10 LinkedList::LinkedList(const LinkedList& src)
 11 {
 12     head = NULL;
 13     NodeType* srccur; //node that is currently in src
 14     srccur = src.head;
 15 
 16     NodeType* pre = NULL; //predecessor of the new node
 17     while (srccur != NULL) // have not finished yet
 18     {   NodeType* newnode;
 19         newnode = new NodeType;
 20         if (newnode == NULL) //dynamic allocation failed
 21             { cout << "Memory allocation error" << endl;
 22                 exit(1);
 23             }
 24 
 25     newnode->value = srccur->value;
 26 
 27     if (pre == NULL)    //the new node becomes the 1st node
 28         head = newnode;
 29     else    // the new node is attached to the end
 30         pre->next = newnode;
 31 
 32     newnode->next = NULL;
 33 
 34     pre = newnode;
 35     srccur = srccur->next;
 36     }
 37 };

Here is the method in the linkedList.cpp file for the second error:

 53 bool LinkedList::insertFirst(string v){
 54     NodeType* newnode = new NodeType;
 55 
 56     if (newnode == NULL) // dynamic allocation failed
 57     {
 58         return false;
 59     }
 60 
 61     newnode->value = v;
 62     newnode->next = head;
 63 
 64     head = newnode;
 65 
 66     return true;
 67 };
Sergei Wallace
  • 1,129
  • 4
  • 16
  • 26

1 Answers1

6

Very simple. Look at your constructor for NodeType (linkedList.h, line 10). It requires a single string. Yet, when you create new instances of NodeType (linkedList.cpp, line 18, line 54), you are calling the constructor without any arguments.

The proper way to do this would be...

NodeType* newnode = new NodeType("Some string.");

If you want that string argument in the constructor to be optional, you can do so by changing the constructor's header prototype to be...

NodeType(std::string str="Some default value");

Also, you could overload your constructor, having one for no arguments, one accepting a string, and so on.


Just so you know, that was a very basic principle. If you are a student (officially or unofficially), you'll want to review C++ Object Oriented Programming, specifically "Constructors".

CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
  • 2
    Yeah... I learned Python first and will be the first to admit that my understanding of C++ is very incomplete at this point. Thanks for the informative answer. I'll take your advice, as well. – Sergei Wallace Oct 12 '15 at 23:00
  • @SergeiWallace, I made that transition as well, and picked up on OOP in C++. This is helpful: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – CodeMouse92 Oct 12 '15 at 23:35