J'écris donc un programme qui reçoit une entrée de chaîne de la part de l'utilisateur et effectue des opérations de base telles que getlength, get index, ajoute à une autre chaîne et teste si elle correspond. La plupart de mon code est basé sur l'exemple de code de mon professeur, j'écris juste des méthodes supplémentaires pour effectuer les opérations que j'ai énumérées précédemment. Cependant, je reçois l'erreur de LinkedChar.h et Node.h que j'ai faite à plusieurs reprises mais que je n'ai pas trouvé de solutions. Je noterai les grosses ERREURS ! mot dans la section commentaire de mon 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
0
FloydVu0531 20 févr. 2020 à 11:24

1 réponse

Meilleure réponse

Vous êtes submergé par les messages d'erreur. Vous devriez prendre chacun à son tour, en commençant par le premier. Comprenez ce qu'il vous dit. Corrigez-le, recompilez pour vous en assurer. Et passez au suivant.

Pour commencer:

  1. Faire length() const

    int length() const {
    
  2. Changement

    next(nullptr);
    

    à

    setNext(nullptr);
    
  3. Changement

    item(anItem);
    

    à

    setItem(anItem);
    
1
acraig5075 20 févr. 2020 à 08:43