Thread: instantiated from here: errors...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    instantiated from here: errors...

    Yes, you all should know my code by know :P Here it is again, changed once again:

    Code:
    SortedLinkedList.h
    
    #include "ItemType.h"
    
    template<class ItemType>
    struct NodeType;
    
    template<class ItemType> 
    class SortedType
    {
     public:
      SortedType();
      // Constructor
      // Postcondition: Empty list is created
    
      ~SortedType();
      // Destructor
      // Postcondition: List is destroyed
    
      SortedType(const SortedType& otherList);
      // Copy-constructor
      // Postcondition: List is created as a duplicate of otherList
    
      bool IsFull() const;
      // Determines whether list is full.
      // Post: Function value = (list is full)
    
      int  LengthIs() const;
      // Determines the number of elements in list.
      // Post: Function value = number of elements in list.
    
      void MakeEmpty();
      // Initializes list to empty state.
      // Post:  List is empty.
    
      void RetrieveItem(ItemType& item, bool& found);
      // Retrieves list element whose key matches item's key 
      // (if present).
      // Pre:  Key member of item is initialized.
      // Post: If there is an element someItem whose key matches 
      //       item's key, then found = true and item is a copy 
      //       of someItem; otherwise found = false and item is 
      //       unchanged. 
      //       List is unchanged.
    
      void InsertItem(ItemType item); 
      // Adds item to list.
      // Pre:  List is not full.
      //       item is not in list. 
      // Post: item is in list 
      // (added)  //       list order is preserved
    
      void DeleteItem(ItemType item);
      // Deletes the element whose key matches item's key.
      // Pre:  Key member of item is initialized.
      //       One and only one element in list has a key matching
      //       item's key.
      // Post: No element in list has a key matching item's key.
    
      void ResetList();
      // Initializes current position to end of list for iteration 
      // through list.
      // Post: Current position is at end of list.
    
      void GetNextItem(ItemType& item);
      // Gets the next element in list.
      // Pre:  
      // Post: If Current position is at end,  currentPos points to head of list, 
      //       else item is  copy  of  element at current position. 
      //       Advance current position.
    
      void Print(); 
    
     private:
      NodeType<ItemType>* listData;
      int length;
      NodeType<ItemType>* currentPos;
    };
     
    template<class ItemType>
    struct NodeType
    {
      ItemType info;
      NodeType* next;
    };
    
    template<class ItemType>
    SortedType<ItemType>::SortedType()  // Class constructor
    {
      length = 0;
      listData = NULL;
    }
    
    template<class ItemType>
    SortedType<ItemType>::~SortedType()
    // Post: List is empty; all items have been deallocated.
    {
      MakeEmpty();
    }
    
    template<class ItemType>
    SortedType<ItemType>::SortedType(const SortedType& otherList)
    // Copy-constructor
    // Postcondition:
    //     IF otherList.listData == NULL
    //         listData == NULL
    //     ELSE
    //         listData points to a new linked list that is a copy of
    //         the linked list pointed to by otherList.listData
    {
        NodeType<ItemType>* fromPtr;  // Pointer into list being copied from
        NodeType<ItemType>* toPtr;    // Pointer into new list being built
        if(otherList.listData == NULL)
        {
            listData = NULL;
            return;
        }
        // Copy first node
        fromPtr = otherList.listData;
        listData = new NodeType<ItemType>;
        listData->info = fromPtr->info;
    
        // Copy remaining nodes
    
        toPtr = listData;
        fromPtr = fromPtr->next;
        while (fromPtr != NULL)
        {
            toPtr->next = new NodeType<ItemType>;
            toPtr = toPtr->next;
            toPtr->info = fromPtr->info;
            fromPtr = fromPtr->next;
        }
        toPtr->next = NULL;
    }
    
    /* bool SortedType::IsFull() const
    // Returns true if there is no room for another ItemType
    //  on the free store; false otherwise.
    {
      NodeType* location;
      try
      {
        location = new NodeType;
        delete location;
        return false;
      }
      catch(bad_alloc exception)
      {
        return true;
      }
    } */
    
    template<class ItemType>
    bool SortedType<ItemType>::IsFull() const
    // Returns true if there is no room for another ItemType
    // object on the free store; false otherwise.
    {
      NodeType<ItemType>* ptr;
    
      ptr = new NodeType<ItemType>;
      if (ptr == NULL)
        return true;
      else
      {
        delete ptr;
        return false;
      }
    }
    
    template<class ItemType>
    int SortedType<ItemType>::LengthIs() const
    // Post: Number of items in the list is returned.
    {
      return length;
    }
    
    template<class ItemType>
    void SortedType<ItemType>::MakeEmpty()
    // Post: List is empty; all items have been deallocated.
    {
      NodeType<ItemType>*
      tempPtr;
    
      while (listData != NULL) // traverse list, deallocating each node in turn
      {
        tempPtr = listData;
        listData = listData->next;
        delete tempPtr;
      }
      length = 0; // to agree with the fact that all nodes are deallocated
    }
    
    template<class ItemType>
    void SortedType<ItemType>::DeleteItem(ItemType item)
    // Pre:  item's key has been initialized.
    //       An element in the list has a key that matches item's.
    // Post: No element in the list has a key that matches item's.
    {
      NodeType<ItemType>* location = listData;
      NodeType<ItemType>* tempLocation;
    
      // Locate node to be deleted.
      if (item == listData->info)  {
        tempLocation = location;
        listData = listData->next;          // Delete first node.
      }
      else
      {
        while (!(item==(location->next)->info))
          location = location->next;
    
        // Delete node at location->next
        tempLocation = location->next;
        location->next = (location->next)->next;
      }
      delete tempLocation;
      length--;
    }
    
    template<class ItemType>
    void SortedType<ItemType>::ResetList()
    // Post: Current position has been initialized to AtEnd
    {
      currentPos = NULL;
    }
    
    template<class ItemType>
    void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found)
    {
      bool moreToSearch;
      NodeType<ItemType>* location;
    
      location = listData;
      found = false;
      moreToSearch = (location != NULL);
    
      while (moreToSearch && !found)
      {
        if (location->info < item)
        {
          location = location->next;
          moreToSearch = (location != NULL);
        }
        else if (item == location->info)
        {
          found = true;
          item = location->info;
        }
        else
          moreToSearch = false;
      }
    }
    
    template<class ItemType>
    void SortedType<ItemType>::InsertItem(ItemType item)
    {
      NodeType<ItemType>* newNode;  // pointer to node being inserted
      NodeType<ItemType>* predLoc;  // trailing pointer
      NodeType<ItemType>* location; // traveling pointer
      bool moreToSearch;
    
      location = listData;
      predLoc = NULL;
      moreToSearch = (location != NULL);
    
      // Find insertion point.
      while (moreToSearch)
      {
        if (location->info < item)
        {
          predLoc = location;
          location = location->next;
          moreToSearch = (location != NULL);
        }
        else
          moreToSearch = false;
     }
      // Prepare node for insertion
      newNode = new NodeType<ItemType>;
      newNode->info = item;
    
      // Insert node into list.
      if (predLoc == NULL)         // Insert as first
      {
        newNode->next = listData;
        listData = newNode;
      }
      else
      {
        newNode->next = location;
        predLoc->next = newNode;
      }
      length++;
    }
    
    template<class ItemType>
    void SortedType<ItemType>::GetNextItem(ItemType& item)
    // Pre:  Current position is defined.
    //       Element at current position is not last in list.
    // Post: Current position has been updated; item is current item.
    {
      if (currentPos == NULL) //Wrap at end of list
        currentPos = listData;
    
      item = currentPos->info;
      currentPos = currentPos->next;
    }
    
    template<class ItemType>
    void SortedType<ItemType>::Print()
    {
       currentPos = listData;
    
       while(currentPos != NULL)
       {
           cout << currentPos->info << " ";
           currentPos = currentPos->next;
       }
    }
    
    runSortedLinkedList.cxx
    #include "SortedLinkedList.h"
    #include <fstream>
    
    using namespace std;
    template<class ItemType>
    void FileToList(SortedType<ItemType>& intList, ifstream& inFile, ofstream&
    outFile);
    // PURPOSE: To take an item from the GetItemFromFile function, and to add
    // it if addOrDelete was 'A' or delete it if addOrDelete was 'D'. Also to
    // print error messages accordingly.
    // INPUT: intList, inFile
    // PRE: intList is declared, inFile is OK, outFile is OK
    // OUTPUT: outFile
    // POST: An item(s) were written to intList with an error message if
    // according
    // NOTE: none
    
    template <class ItemType>
    void ListToFile(SortedType<ItemType>& intList, ofstream& outFile);
    // PURPOSE: To print the list to a file as long as data exists in the list
    // INPUT: intList
    // PRE: intList is declared and OK, outFile is OK
    // OUTPUT: outFile
    // POST: All items are written to the outFile
    // NOTE: none
    
    int main()
    {
       ifstream inFile;
       ofstream outFile;
       SortedType<int> intList;
    
       FileToList(intList, inFile, outFile);
       ListToFile(intList, outFile);
    
       return 0;
    }
    
    
    void FileToList(SortedType<ItemType>& intList, ifstream& inFile, ofstream&
    outFile)
    {
       ItemType item;
       char addOrDelete;
    
       inFile.open("in.data");
       intList.MakeEmpty();
       addOrDelete = item.GetItemFromFile(inFile);
       while (inFile)
       {
          if (addOrDelete == 'A')
          {
             if (!intList.IsFull())
                intList.InsertItem(item);
             else{
                ListToFile(intList, outFile);
                item.PrintLstFullErr(outFile);
             }
             addOrDelete = item.GetItemFromFile(inFile);
          }
          else if (addOrDelete == 'D')
          {
             if(intList.LengthIs() == 0){
                ListToFile(intList, outFile);
                item.PrintEmptyLstErr(outFile);
             }
             else
             {
                intList.DeleteItem(item);
             }
             addOrDelete = item.GetItemFromFile(inFile);
          }
       }
    }
    
    void ListToFile(SortedType<ItemType>& intList, ofstream& outFile)
    {
       ItemType item;
       int length;
    
       outFile.open("out.data");
       intList.ResetList();
       length = intList.LengthIs();
    
       outFile << "STUDENT ID" << setw(6) << "EXAM1" << setw(10) << "EXAM2"
               << setw(9) << "FINAL" << setw(9) << "PROG" << setw(14)
               << "QUIZZES" << endl;
       for (int count = 1; count <= length; count++)
       {
          intList.GetNextItem(item);
          item.WriteItemToFile(outFile);
       }
    }
    Well, here are my new errors with this, and I have outlined them in the code with red:

    SortedLinkedList.h: In member function `void
    SortedType<ItemType>::InsertItem(ItemType) [with ItemType = ItemType]':
    runSortedLinkedList.cxx:69: instantiated from here
    SortedLinkedList.h:269: error: no match for 'operator<' in '
    location->NodeType<ItemType>::info < item'
    SortedLinkedList.h: In member function `void
    SortedType<ItemType>::DeleteItem(ItemType) [with ItemType = ItemType]':
    runSortedLinkedList.cxx:84: instantiated from here
    SortedLinkedList.h:202: error: no match for 'operator==' in 'item ==
    this->SortedType<ItemType>::listData->NodeType<ItemType>::info'
    SortedLinkedList.h:209: error: no match for 'operator==' in 'item ==
    location->NodeType<ItemType>::next->NodeType<ItemType>::info'

    And that's it. I think once this works, everything will, i'm just down to the last part. I know you are all probably sick of hearing form me :P but this I swear will be the last time that I bug you :). If you know how I can re-code or change a few things to get this to work, let me know. Thanks everyone!

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    That is fixed, I had to add template <class ItemType> to the last two functions; but now I get these errors:
    runSortedLinkedList.cxx: In function `void FileToList(SortedType<ItemType>&,
    std::ifstream&, std::ofstream&) [with ItemType = int]':
    runSortedLinkedList.cxx:50: instantiated from here
    runSortedLinkedList.cxx:66: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `int'
    runSortedLinkedList.cxx:50: instantiated from here
    runSortedLinkedList.cxx:75: error: request for member `PrintLstFullErr' in `
    item', which is of non-aggregate type `int'
    runSortedLinkedList.cxx:77: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `int'
    runSortedLinkedList.cxx:50: instantiated from here
    runSortedLinkedList.cxx:83: error: request for member `PrintEmptyLstErr' in `
    item', which is of non-aggregate type `int'
    runSortedLinkedList.cxx:50: instantiated from here
    runSortedLinkedList.cxx:89: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `int'
    runSortedLinkedList.cxx: In function `void ListToFile(SortedType<ItemType>&,
    std::ofstream&) [with ItemType = int]':
    runSortedLinkedList.cxx:51: instantiated from here
    runSortedLinkedList.cxx:110: error: request for member `WriteItemToFile' in `
    item', which is of non-aggregate type `int'

    These are all errors when i am trying to call a function from ItemType. Thanks.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    when you see non-aggregate it typically means you're doing something like this

    Code:
    int a;
    a.somefunction();
    or

    Code:
    SomeClass *c;
    c.someFunction() //where is should be c->someFunction();
    by the way - i didn't look at your code - just guessing
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Nope, all my functions that are called are of ItemType (declared ItemType item), and then are called with item.FunctionName();

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    don't tell me 'nope'.

    Code:
    SortedType<int> intList;
    ..makes ItemType an int.

    you're first non-aggregate

    runSortedLinkedList.cxx:66: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `int'

    is caused by
    Code:
    addOrDelete = item.GetItemFromFile(inFile);
    'item' is declare as
    Code:
    ItemType item;
    therefore saying (more or less)
    Code:
    int.GetItemFromFile(inFile);
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Ok, you're right, I just don't know hot to really use templates that well :P This is my first time implementing them. But do you know how I could fix this problem? I am trying to input everything into ItemType, and then print them out of ItemType, but it doesn't appear that I can do that. The only problem is that if I read them into something other than ItemType, will SortedType even work right? Any idea's on how to re-code or fix this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  4. MinGW, standard library, pain.
    By Tonto in forum Tech Board
    Replies: 9
    Last Post: 08-26-2006, 10:46 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM