2

私の課題は、再帰を使用して並べ替えられたリンク リストを追跡するコードを作成することです。クラスとファイルの構成は与えられましたが、機能を埋める必要がありました。パブリック関数でプライベート関数を呼び出したために発生したと思われる 4 つのエラーが発生しています。私のエラーは次のとおりです。

1>SortedList.obj : error LNK2019: unresolved external symbol "public: void __thiscall SortedList::recursiveMakeEmpty(class NodeType * &)" (?recursiveMakeEmpty@SortedList@@QAEXAAPAVNodeType@@@Z) referenced in function "public: void __thiscall SortedList::makeEmpty(void)" (?makeEmpty@SortedList@@QAEXXZ)
1>SortedList.obj : error LNK2019: unresolved external symbol "public: bool __thiscall SortedList::recursiveInsert(class NodeType * &,int)" (?recursiveInsert@SortedList@@QAE_NAAPAVNodeType@@H@Z) referenced in function "public: bool __thiscall SortedList::insert(int)" (?insert@SortedList@@QAE_NH@Z)
1>SortedList.obj : error LNK2019: unresolved external symbol "public: bool __thiscall SortedList::recursiveDelete(class NodeType * &,int)" (?recursiveDelete@SortedList@@QAE_NAAPAVNodeType@@H@Z) referenced in function "public: bool __thiscall SortedList::deleteItem(int)" (?deleteItem@SortedList@@QAE_NH@Z)
1>SortedList.obj : error LNK2019: unresolved external symbol "public: void __thiscall SortedList::recursivePrint(class NodeType * &)" (?recursivePrint@SortedList@@QAEXAAPAVNodeType@@@Z) referenced in function "public: void __thiscall SortedList::printList(void)" (?printList@SortedList@@QAEXXZ)
1>C:\Users\Ross\documents\visual studio 2010\Projects\Recursive Sorted List\Debug\Recursive Sorted List.exe : fatal error LNK1120: 4 unresolved externals

私のコード:

Node.h

typedef int ItemType;

class NodeType
    {
    public:
        ItemType info;
        NodeType *next;
    };

SortedList.h

#include "MyClass.h"

string MyClass::foo = "bar"          #pragma once

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

      using namespace std;

      class SortedList 
      {
      public:
        SortedList();
        // Constructor used before any operations are done on the list.  Initializes the list to empty.
        // Preconditions: None.
        // Postconditions: List is an empty list.

        ~SortedList();
        // Destructor used when a List is no longer in scope.
        // Precondition: None.
        // Postcondition: No dynamic variables exist.

        void makeEmpty();
        // Makes the list empty if it is not empty already.
        // Preconditions: The list may be empty or have items in it.
        // Postconditions: List is now an empty list.  Any dynamically allocated memory which is no longer used is returned to the system. 
  The current position is 1.
        // Note: This function body is different from the constructor.

        bool isEmpty();
        // Returns True if List is an empty list; return False otherwise.
        // Postconditions: List remains unchanged.

        bool isFull();
        // Returns True if List is full; returns False other wise.
        // Postcondition: List remains unchanged.
        // Note: For linked list implementation, you can simply return false.

        bool insert( ItemType newItem );
        // Inserts the given newItem in the list maintaining sorted order.
        // Preconditions: The list may be empty.
        // Postconditions: If the list already contained a matching item, the function returned false and the list is unchanged.
        // Otherwise the newItem is inserted in the linked list so that the list maintains sorted order, and true is returned.

        bool deleteItem( ItemType deleteItem );
        // Deletes the item from the linked list if it is found.
        // Preconditions: List may be empty.  At most one matching item is in the list.
        // Postconditions: If the itemwas in the list, it is deleted and true is returned.  If the item was not in the list, the list is
  unchanged and false is returned.

        void printList();
        // Prints items in the list
        // Preconditions: Assumes that the operator << has been defined for ItemType.
        // Postcondition: Items on the list have been printed to the standard output.  List is unchanged.

      private:
        NodeType *head;
        // Points to head of linked list.

        void recursiveMakeEmpty( NodeType *& node);
        // Makes the list empty if it is not empty already.
        // Preconditions: The list may be empty or have items in it.
        // Postconditions: List is now an empty list.  Any dynamically allocated memory which is no longer used is returned to the system. 
  The current position is 1.

        bool recursiveInsert( NodeType *& node, ItemType newItem );
        // Inserts the given newItem in the list maintaining sorted order.
        // Preconditions: The list may be empty.
        // Postconditions: If the list already contained a matching item, the function returned false and the list is unchanged.
        // Otherwise the newItem is inserted in the linked list so that the list maintains sorted order, and true is returned.

        bool recursiveDelete( NodeType *& node, ItemType deleteItem );
        // Deletes the item from the linked list if it is found.
        // Preconditions: List may be empty.  At most one matching item is in the list.
        // Postconditions: If the itemwas in the list, it is deleted and true is returned.  If the item was not in the list, the list is
  unchanged and false is returned.

        void recursivePrint( NodeType *& node );
        // Prints items in the list
        // Preconditions: Assumes that the operator << has been defined for ItemType.
        // Postcondition: Items on the list have been printed to the standard output.  List is unchanged.
      };

SortedList.cpp

#include "SortedList.h"

SortedList::SortedList()
// Constructor used before any operations are done on the list.  Initializes the list to empty.
// Preconditions: None.
// Postconditions: List is an empty list.
{
    head = NULL;
}

SortedList::~SortedList()
// Destructor used when a List is no longer in scope.
// Precondition: None.
// Postcondition: No dynamic variables exist.
{
    makeEmpty();
}

void SortedList::makeEmpty()
// Makes the list empty if it is not empty already.
// Preconditions: The list may be empty or have items in it.
// Postconditions: List is now an empty list.  Any dynamically allocated memory which is no longer used is returned to the system.  The current position is 1.
// Note: This function body is different from the constructor.
{
    recursiveMakeEmpty(head);
}


bool SortedList::isEmpty()
// Returns True if List is an empty list; return False otherwise.
// Postconditions: List remains unchanged.
{
    return ( head == NULL );
}
//
bool SortedList::isFull()
// Returns True if List is full; returns False other wise.
// Postcondition: List remains unchanged.
// Note: For linked list implementation, you can simply return false.
{
    return false;
}

bool SortedList::insert( ItemType newItem )
// Inserts the given newItem in the list maintaining sorted order.
// Preconditions: The list may be empty.
// Postconditions: If the list already contained a matching item, the function returned false and the list is unchanged.
// Otherwise the newItem is inserted in the linked list so that the list maintains sorted order, and true is returned.
{
    return recursiveInsert( head, newItem );
}

bool SortedList::deleteItem( ItemType deleteItem )
// Deletes the item from the linked list if it is found.
// Preconditions: List may be empty.  At most one matching item is in the list.
// Postconditions: If the itemwas in the list, it is deleted and true is returned.  If the item was not in the list, the list is unchanged and false is returned.
{
    return recursiveDelete( head, deleteItem );
}

void SortedList::printList()
// Prints items in the list
// Preconditions: Assumes that the operator << has been defined for ItemType.
// Postcondition: Items on the list have been printed to the standard output.  List is unchanged.
{
    recursivePrint( head );
}

bool recursiveInsert( NodeType *& node, ItemType newItem )
// Inserts the given newItem in the list maintaining sorted order.
// Preconditions: The list may be empty.
// Postconditions: If the list already contained a matching item, the function returned false and the list is unchanged.
// Otherwise the newItem is inserted in the linked list so that the list maintains sorted order, and true is returned.
{
    if( node == NULL )
    {
        node = new NodeType;
        node->info = newItem;
        node->next = NULL;
        return true;
    }
    else if( node->info == newItem )
    {
        return false;
    }
    else if( newItem < node->next->info )
    {
        NodeType* ptr = new NodeType;
        ptr->info = newItem;
        ptr->next = node->next;
        node->next = ptr;
        return true;
    }
    else
    {
        recursiveInsert( node->next, newItem );
    }
}

bool recursiveDelete( NodeType *& node, ItemType deleteItem )
    // Deletes the item from the linked list if it is found.
    // Preconditions: List may be empty.  At most one matching item is in the list.
    // Postconditions: If the itemwas in the list, it is deleted and true is returned.  If the item was not in the list, the list is unchanged and false is returned.
{
    if( node == NULL )
    {
        return false;
    }
    else if( node->next->info == deleteItem )
    {
        NodeType* ptr = node->next->next;
        delete node->next;
        node->next = ptr;
        return true;
    }
    else
    {
        recursiveDelete( node->next, deleteItem );
    }
}

void recursiveMakeEmpty( NodeType *& node)
// Makes the list empty if it is not empty already.
// Preconditions: The list may be empty or have items in it.
// Postconditions: List is now an empty list.  Any dynamically allocated memory which is no longer used is returned to the system.  The current position is 1.
{
    if( node == NULL )
    {
        return;
    }
    else
    {
        recursiveDelete( node, node->info );
    }
}

void recursivePrint( NodeType *& node )
// Prints items in the list
// Preconditions: Assumes that the operator << has been defined for ItemType.
// Postcondition: Items on the list have been printed to the standard output.  List is unchanged.
{
    if( node == NULL )
    {
        return;
    }
    else if( node->next == NULL )
    {
        cout << node->info << "\n\n";
    }
    else
    {
        cout << node->info << ", ";
        recursivePrint(node->next);
    }
}

main.cpp

#include "SortedList.h"

void menuSelection(char &selection);

int main()
{
    char selection = 'S';
    SortedList List;
    ItemType item;

    do
    {
        menuSelection(selection);

        switch( selection )
        {
        case 'A':
            cout << "What item would you like to add?   ";
            cin >> item;
            if (List.insert(item))
            {
                cout << "\n Item has been added! \n\n";
            }
            else
            {
                cout << "\n Item already in list! \n\n";
            }
            break;
        case 'D':
            cout << "What item would you like to delete?    ";
            cin >> item;
            if (List.deleteItem(item))
            {
                cout << "\n Item has been deleted! \n\n";
            }
            else
            {
                cout << "\n Item was not found! \n\n";
            }
            break;
        case 'M':
            List.makeEmpty();
            cout << "\n List is now empty! \n\n";
            break;
        case 'P':
            cout << "\n The list is:  ";
            List.printList();
            cout << "\n\n";
            break;
        }
    } while( selection != 'Q' );


    return 0;
}

void menuSelection(char &selection)
{
    cout << "What would you like to do? \n\n";
    cout << "A - add item. \n";
    cout << "D - delete item. \n";
    cout << "M - make empty. \n";
    cout << "P - print list. \n";
    cout << "Q - quit. \n\n";
    do
    {
        cin >> selection;
        if( selection != 'A' && selection != 'D' && selection != 'M' && selection != 'P' && selection != 'Q' )
        {
            cout << "Must enter A, D, M, P, or Q.";
        }
    } while( selection != 'A' && selection != 'D' && selection != 'M' && selection != 'P' && selection != 'Q' );
}
4

1 に答える 1

12

SortedList::メソッドでさえ、すべてのメソッド定義の前に追加する必要がありprivateます。

于 2012-11-07T17:40:02.960 に答える