0

Trying to copy a LinkedList of ItemType.

Excerpt from main.cpp

LinkedList<ItemType> LLobject;
// codes that insert data to object ItemType
LinkedList<ItemType> copyLLobject(LLobject);

Excerpt from Node.cpp

template<class ItemType>
Node<ItemType>::Node() : next(0) { } // ERROR ACKNOWLEDGED BY COMPILER

Error message from compiler

error: no matching function for call to 'ItemType::ItemType()'

If it helps, here are the codes for LinkedList constructor in LinkedList.cpp

template<class ItemType>
LinkedList<ItemType>::LinkedList(const LinkedList<ItemType>& aList) : itemCount(aList.itemCount)
{
   Node<ItemType>* origChainPtr = aList.headPtr;  // Points to nodes in original chain

   if (origChainPtr == 0)
      headPtr = 0;  // Original list is empty
   else
   {
      // Copy first node
      headPtr = new Node<ItemType>();
      headPtr->setItem(origChainPtr->getItem());

      // Copy remaining nodes
      Node<ItemType>* newChainPtr = headPtr;      // Points to last node in new chain
      origChainPtr = origChainPtr->getNext();     // Advance original-chain pointer
      while (origChainPtr != 0)
      {
         // Get next item from original chain
         ItemType nextItem = origChainPtr->getItem();

         // Create a new node containing the next item
         Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);

         // Link new node to end of new chain
         newChainPtr->setNext(newNodePtr);

         // Advance pointer to new last node
         newChainPtr = newChainPtr->getNext();

         // Advance original-chain pointer
         origChainPtr = origChainPtr->getNext();
      }  // end while

      newChainPtr->setNext(0);              // Flag end of chain
   }  // end if
}  // end copy constructor

EDIT:

I have two constructors of ItemType in ItemType.cpp

Default Constructor

ItemType::ItemType() { }

Overloading Constructor

ItemType::ItemType( string t = "", string q = "", string a = "", int p = 0 )
        : t(t), q(q), a(a), p(p) { }
John Escobia
  • 450
  • 1
  • 5
  • 14
  • 4
    Sooo... Do you have a default constructor for ItemType? – Darktega Feb 10 '17 at 04:30
  • 1
    Are you sure it's problem with a copy constructor of the list? Looks like there is a problem with `ItemType` not having a default constructor. And in the line from `Node.cpp` it can be seen that field of `Node` of type `ItemType` is not initialized explicitly, so compiler tries to call default constructor. – yeputons Feb 10 '17 at 04:30
  • Yes, I do have a default constructor for ItemType. Will add in the question. – John Escobia Feb 10 '17 at 04:33
  • 2
    That's not a default constructor. – Mikel F Feb 10 '17 at 04:39
  • 1
    You can't have default values in the definition when the definition is not inline. Only the declaration may have default values. Do you have the default values in the declaration also? – R Sahu Feb 10 '17 at 04:39
  • @MikelF Thanks for correcting. I've edited the default constructor above, but it still displays the same error. – John Escobia Feb 10 '17 at 04:44
  • Did that edit move the default-value specification to the class declaration? If not, it *still* won't work. An unrelated, choosing `ItemType` as a hard class *and* a template parameter name is anything-but-intuitive. – WhozCraig Feb 10 '17 at 04:46
  • @RSahu I've tweaked the default constructor. Now it does not initialize values. I initialize the values only in the overloading constructor. – John Escobia Feb 10 '17 at 04:47
  • @JohnEscobia, try to create a [mcve]. You might resolve the problem in the process of doing that. If not, you can post it here. It is much easier to suggest something more useful than vague comments when there is a [mcve]. – R Sahu Feb 10 '17 at 04:50
  • `headPtr = new Node(origChainPtr->getItem());`, and lose the `setitem` call immediately thereafter. That will probably "fix" your problem for now. I harbor doubts you understand what a default-ctor is, and any [good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?s=1|13.4773) – WhozCraig Feb 10 '17 at 04:52
  • @WhozCraig, I moved the default-value specification to the overloaded constructor, still, same error. Do you suggest to initialize the values in the class declaration itself? – John Escobia Feb 10 '17 at 04:53
  • @JohnEscobia -- *Default Constructor* -- A default constructor is one that can be called with no arguments. What you posted is not a default constructor. – PaulMcKenzie Feb 10 '17 at 04:53
  • @PaulMcKenzie, noted and changed accordingly. Thanks for correcting. – John Escobia Feb 10 '17 at 04:58
  • My first suggestion would be to post a [*minimal, **complete**, verifiable example*](https://stackoverflow.com/help/mcve) that produces the problem. Second, stop naming hard classes with the same as template parameters. It does *nothing* but confuse and obfuscate the code. Third, do what was asked and provide a *default* constructor for the class used as your template argument type.. If you don't *honestly* know what that is, then do a little homework; don't just claim you did it when your update is clearly *not* it. Finally, you can't have *both* of those constructors as posted now. – WhozCraig Feb 10 '17 at 04:59
  • @JohnEscobia -- So when you call the constructor with no arguments, which one will the compiler choose? See the problem now? – PaulMcKenzie Feb 10 '17 at 05:00
  • Hey, everyone. I think I might've confused some of you of what the exact problem is. I will do a minimal, complete, verifiable example for this one instead. Thanks for the corrections. – John Escobia Feb 10 '17 at 05:18

0 Answers0