FloydVu0531
FloydVu0531

Reputation: 23

C++ LinkedList problems. Conflict between datatypes? No matching Constructor?

so I'm writing a program that receives string input from user and performs some basic operations on that such as getlength, get index, append to another string and test if it sub match. Most of my code is base on my professor's sample code, I just write extra methods to performs the operations that I listed earlier. However, I receives the error from LinkedChar.h and Node.h that I repeatedly made but couldn't find solutions. I will note the big ERRORS! word in the comment section in my code.

    #include <iomanip>
    #include "LinkedChar.h"


    // Function prototype
    bool isCreated(const LinkedChar<char>* charPtr);
    LinkedChar<char> toLinkedChar(const string &items);

    /************************************************************************
    *                                 main                                  *
    ************************************************************************/
    int main()
    {
        bool done = false; // A finish flag
        int choice; // Menu choice

        cout << " This program solves an ADT character string.  \n";

        LinkedChar<char>* charPtr = nullptr;
        string firstString;
        char aChar;

        while (!done)
        {
            // Display the menu and get the user's choice
            cout << "=========================================================\n"
                << "Operations\n\n";
            cout << setw(5) << "1. New the first string\n";
            cout << setw(5) << "2. Get the length from the first string\n";
            cout << setw(5) << "3. Find index of character in the first string\n";
            cout << setw(5) << "4. Append another string to the first string\n";
            cout << setw(5) << "5. Test if another string is submatch of the first string\n";
            cout << setw(5) << "6. Quit\n"
                <<"=========================================================\n\n";
            cout << "Enter your Choice (1-6): ";
            cin >> choice;
            cin.ignore();

            // Validate and process the menu choice
            if (choice == 1)
            {
                cout << "Please enter a string: ";
                getline(cin, firstString);

                charPtr = new LinkedChar<char>(firstString);
                cout << "The first string has been created. \n\n";
            }
            else if (choice == 6)
            {
                // Set the program is finished
                done = true;
            }
            else if (choice > 1 && choice < 6)
            {
                if (isCreated(charPtr))
                    // Execute the correct set of actions
                    switch (choice)
                    {
                        case 2:
                            cout << "The length of the first string \"" <<  charPtr->toString() << "\" is " << charPtr->length() << ".\n\n";
                            break;
                        case 3:
                            cout << "Please enter a character to find: ";
                            cin >> aChar;
                            cout << "The index of \"" << aChar << "\" is " << charPtr->index(aChar) << "\n\n";
                            break;
                        case 4:
                            cout << "Please enter another string to append: ";
                            getline(cin, firstString);
                            charPtr->append(toLinkedChar(firstString));
                            cout << "The string changes to \"" << charPtr->toString() << "\".\n\n";

                            break;
                        case 5:
                            cout << "Please enter another string to test: ";
                            getline(cin, firstString);
                            cout << "The string \"" << firstString << "\" is ";
                            if (!charPtr->submatch(toLinkedChar(firstString)))
                                cout << "not ";
                            cout << "submatch of the first string \"" << charPtr->toString() << "\".\n\n";
                            break;
                    }
            }
            else
            {
                cout << "The valid choices are 1 through 6.\n\n";
            }
        }

        return 0;
    }

    bool isCreated(const LinkedChar<char>* charPtr)
    {
        if (charPtr == nullptr)
        {
            std::cout << "Please choose 1 to create the first string before start!\n\n";
            return false;
        }
        else
        {
            return true;
        }
    }

    LinkedChar<char> toLinkedChar(const string &items)
    {
        return LinkedChar<char>(items);
    }

Node.h

        #ifndef NODE_H
        #define NODE_H

        template<class ItemType>
        class Node
        {
        private:
            ItemType item;
            Node<ItemType>* next; // Pointer to next node

        public:
            Node() {
                next = nullptr;
            }
            Node(const ItemType& anItem)
            {
                //  ----------- ERRORS ------------// 
                item(anItem);    //Called object type 'char' is not a function or function 
                                 //pointer
                next(nullptr);   //Called object type 'Node<char> *' is not a function or 
                                //function pointer

                                  /*This copy constructor is also exposed an error that I 
                                 could not understand. I think that I declare the item as a 
                                 template so later I could determine that datatype for item is 
                                either string or char*/  
                 // --------------------------------// 
            }
            Node(const ItemType& anItem, Node<ItemType>* nextNodePtr)
            {
                item(anItem);
                next(nextNodePtr);
            }

            void setItem(const ItemType& anItem)
            {
                item = anItem;
            }
            void setNext(Node<ItemType>* nextNodePtr)
            {
                next = nextNodePtr;
            }

            ItemType getItem()  const {
                return item;
            }
            Node<ItemType>* getNext() const {
                return next;
            } 
        }; // end Node
        #endif

LinkedChar.h

        // Created by 
        //
        #ifndef LINKEDCHAR_H
        #define LINKEDCHAR_H

        #include <iostream>
        #include <string>
        #include "Node.h"

        using namespace std;

        template<class ItemType>
        class LinkedChar
        {
        private:
            Node<ItemType>* headPtr;
            int itemCount;
        public:
            LinkedChar() {
                headPtr = nullptr;
                itemCount = 0;
            }
            LinkedChar(string items) {

                itemCount = int(items.size());

                headPtr = nullptr;

                for (int i = itemCount - 1; i >= 0; i--)
                {
                    if (headPtr == nullptr)
                    {
                        // Copy first node
                        headPtr = new Node<ItemType>();
                        headPtr->setItem(items[i]);
                    } else {
                        // Create a new node containing the next item
                        Node<ItemType>* newNodePtr = new Node<ItemType>(items[i]);
                        newNodePtr->setNext(headPtr);  // New node points to chain

                        headPtr = newNodePtr;          // New node is now first node
                    }
                }
            }
            virtual ~LinkedChar() {
                clear();
            }

            int length() {
                return itemCount;
            }
            int index(const ItemType& anItem) const {
                int index = 0;
                Node<ItemType>* curPtr = headPtr;

                while (curPtr != nullptr)
                {
                    if (anItem == curPtr->getItem())
                    {
                        return index;

                    } // end if
                    index++;
                    curPtr = curPtr->getNext();
                }
                return -1;
            }; // -1 if no match
            void append(const LinkedChar& lc) {
                  // -------------- ERRORS ------------------ //

                itemCount += lc.length(); // This is when I got the error guys, it says
                                          // " 'this' argument to member function 'length' has
                                          //type 'const LinkedChar<char>', but function is not
                                           marked const "
                 // ------------------------------------------//
                Node<ItemType>* newChainPtr = headPtr;
                while (newChainPtr != nullptr && newChainPtr->getNext() != nullptr)
                {
                    newChainPtr = newChainPtr->getNext();
                }

                Node<ItemType>* origChainPtr = lc.headPtr;
                while (origChainPtr != nullptr)
                {
                    // Get next item from original chain
                    ItemType nextItem = origChainPtr->getItem();

                    // Create a new node containing the next item

                   // ---------------- ERRORS -------------------- //

                    Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);
                                               ^^
                                               ||
                                               ||
                   // ERROR: The Error of no matching constructor for initialization of Node<Char>
                   //This happens to me alot previously, my solution is just put new Node(' ') 
                   //But in this case seem inappropriate
                   // ---------------------------------------------  //

                    // 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();
                }
                newChainPtr->setNext(nullptr);
            }
            bool submatch(const LinkedChar& lc) const {
                bool found = false;
                Node<ItemType>* curPtr = headPtr;
                Node<ItemType>* origChainPtr = lc.headPtr;

                ItemType anItem;

                while (origChainPtr != nullptr && curPtr != nullptr)
                {
                    anItem = origChainPtr->getItem();

                    while (curPtr != nullptr)
                    {
                        if (anItem == curPtr->getItem())
                        {
                            found = true;
                            curPtr = curPtr->getNext();
                            break;
                        }
                        else if (found)
                        {
                            found = false;
                            break;
                        }

                        curPtr = curPtr->getNext();
                    }
                    if (!found)
                        origChainPtr = lc.headPtr;
                    else
                        origChainPtr = origChainPtr->getNext();
                }

                return found && (origChainPtr == nullptr);
            }

            void clear() {
                Node<ItemType>* curPtr = headPtr;
                while (headPtr != nullptr)
                {
                    headPtr = headPtr->getNext();

                    // Return node to the system
                    curPtr->setNext(nullptr);
                    delete curPtr;

                    curPtr = headPtr;
                }

                itemCount = 0;
            }

            string toString() const {
                std::string aString;
                Node<ItemType>* curPtr = headPtr;

                while ((curPtr != nullptr))
                {
                    aString += (curPtr->getItem());
                    curPtr = curPtr->getNext();
                }

                return aString;
            }
        };

        //#include "LinkedChar.cpp"
        #endif //LINKEDCHAR_H

Upvotes: 0

Views: 78

Answers (1)

acraig5075
acraig5075

Reputation: 10756

You're getting overwhelmed by error messages. You should take each one in turn, starting with the first. Understand what it's telling you. Fix it, recompile to make sure. And move on to the next one.

For starters:

  1. Make length() const

    int length() const {
    
  2. Change

    next(nullptr);
    

    to

    setNext(nullptr);
    
  3. Change

    item(anItem);
    

    to

    setItem(anItem);
    

Upvotes: 1

Related Questions