Thread: Conflicting types of typedef error

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

    Conflicting types of typedef error

    I am having a problem with my code. Here are the error messages:

    In file included from runSortedLinkedList.cxx:18:
    SortedLinkedList.h:2: error: conflicting types for `typedef int ItemType'
    ItemType.h:9: error: previous declaration as `class ItemType'

    runSortedLinkedList.cxx: In function `void FileToList(SortedType&,
    std::ifstream&, std:fstream&)':
    runSortedLinkedList.cxx:62: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `ItemType'
    runSortedLinkedList.cxx:71: error: request for member `PrintLstFullErr' in `
    item', which is of non-aggregate type `ItemType'
    runSortedLinkedList.cxx:73: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `ItemType'
    runSortedLinkedList.cxx:79: error: request for member `PrintEmptyLstErr' in `
    item', which is of non-aggregate type `ItemType'
    runSortedLinkedList.cxx:85: error: request for member `GetItemFromFile' in `
    item', which is of non-aggregate type `ItemType'
    runSortedLinkedList.cxx: In function `void ListToFile(SortedType&,
    std:fstream&)':
    runSortedLinkedList.cxx:105: error: request for member `WriteItemToFile' in `
    item', which is of non-aggregate type `ItemType'

    All of which i believe is the problem of what is highlighted in red. Here is my code:

    Code:
    ItemType.h
    
    #include <fstream.h>
    #include <iostream.h>
    #include <iomanip.h>
    
    const int MAX_ITEMS = 10;
    enum  RelationType {LESS, EQUAL, GREATER};
    
    class ItemType
    {						
      public :
        
        ItemType();
    //  PURPOSE: To set each variable with a default value
    //  INPUT: none
    //  PRE: all variables exist
    //  OUTPUT: none
    //  POST: default data is assigned
    //  NOTE: none
    
        ItemType(int id, float exam1, float exam2, float final, float prog, 
        float quizzes, char addOrDelete);   
    //  PURPOSE: This is used to initialize values to variables
    //  INPUT: id, exam1, exam2, final, prog, quizzes, addOrDelete
    //  PRE: all variables are assinged and ok
    //  OUTPUT: none
    //  POST: id, exam1, exam2, final, prog, quizzes, and addOrDelete are 
    //  assinged
    //  NOTE: none
    
        RelationType ComparedTo(ItemType) const;
    //  PURPOSE: This comapres one item of ItemType to another item to return 
    //  either greater, less, or equal
    //  INPUT: ItemType
    //  PRE: ItemType is valid and assinged
    //  OUTPUT: LESS, GREATER, or EQUAL
    //  POST: That less, greater, or equal is returned
    //  NOTE: none
    
        char GetItemFromFile(ifstream& inFile);
    //  PURPOSE: This will read the items from an inFile, and will also 
    //  determine what variables to read by it's first character
    //  INPUT: inFile
    //  PRE: inFile is opened and OK
    //  OUTPUT: addOrDelete
    //  POST: all variables are assinged and addOrDelete is returned
    //  NOTE: none
     
        void WriteItemToFile(ofstream& outFile) const;
    //  PURPOSE: This will write all variables (id, exam1, exam2, final, prog, 
    //  and quizzes, to an outFile
    //  INPUT: none
    //  PRE: outFile is opened and OK
    //  OUTPUT: outFile
    //  POST: all variables are assigned to the outFile
    //  NOTE: none
    
        void PrintLstFullErr(ofstream& outFile) const;
    //  PURPOSE: This will print the error message when the list is full and 
    //  is trying to be added too
    //  INPUT: none
    //  PRE: list is full and trying to be written to
    //  OUTPUT: outFile
    //  POST: error message is written to outFile
    //  NOTE: none
    
        void PrintEmptyLstErr(ofstream& outFile);
    //  PURPOSE: This will print an error message when the list is empty but
    //  a line is trying to be deleted from it
    //  INPUT: none
    //  PRE: list is empty and is trying to be deleted from
    //  OUTPUT: outFile
    //  POST: error message is written to the outFile
    //  NOTE: none
    
      private :		
        int id;
        float exam1;
        float exam2;
        float final;
        float prog;
        float quizzes;
        char addOrDelete;       
    } ;	
    
    SortedLinkedList.h
    
    #include "ItemType.h"
    typedef int ItemType;	// Node definition
    
    struct NodeType
    {
      ItemType info;
      NodeType* next;
    };
     
    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* listData;
      int length;
      NodeType* currentPos;
    };
    
    
    runSortedLinkedList.cxx
    
    #include "SortedLinkedList.h"
    #include <fstream>
    
    using namespace std;
    void FileToList(SortedType& 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
    
    void ListToFile(SortedType& 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 intList;
    
       FileToList(intList, inFile, outFile);
       ListToFile(intList, outFile);
    
       return 0;
    }
    
    void FileToList(SortedType& 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& 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);
       }
    }
    If you need any of the .cxx for the ItemType or the SortedLinkedList let me know. Any help on this situation and how i can fix it would be greatly appreciated. Thank you!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How is the compiler supposed to know if your ItemType objects need to be a class or an int? Either rename the class to something else, or rename the typedef.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >class ItemType
    You define ItemType as a class.

    >typedef int ItemType; // Node definition
    Then you decide, no, ItemType is an int. Perhaps you should remove the typedef.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Ok, I have now changed the ItemType class over to a class called TestType. I now have the following errors:
    runSortedLinkedList.cxx: In function `void FileToList(SortedType&,
    std::ifstream&, std:fstream&)':
    runSortedLinkedList.cxx:67: error: no matching function for call to `SortedType
    ::InsertItem(TestType&)'
    SortedLinkedList.h:47: error: candidates are: void SortedType::InsertItem(int)
    runSortedLinkedList.cxx:82: error: no matching function for call to `SortedType
    :eleteItem(TestType&)'
    SortedLinkedList.h:54: error: candidates are: void SortedType:eleteItem(int)
    runSortedLinkedList.cxx: In function `void ListToFile(SortedType&,
    std:fstream&)':
    runSortedLinkedList.cxx:103: error: no matching function for call to `
    SortedType::GetNextItem(TestType&)'
    SortedLinkedList.h:67: error: candidates are: void
    SortedType::GetNextItem(ItemType&)

    I understand the errors, i just don't understand how to fix them. I need more help! It just doesn't like me. I will now include all my cxx files to make more sense of this.

    Code:
    ItemType.cxx
    
    #include  "ItemType.h"
    
    TestType::TestType()
    //  PURPOSE: To set each variable with a default value
    //  INPUT: none
    //  PRE: all variables exist
    //  OUTPUT: none
    //  POST: default data is assigned
    //  NOTE: none
    {
      id = -111;
      exam1 = -1.0;
      exam2 = -1.1;
      final = -2.0;
      prog = -3.0;
      quizzes = -4.0;
      addOrDelete = 'p';
    }   	
    
    TestType::TestType( int d, float g, float h, float i, float j, float k, 
    char p)
    //  PURPOSE: This is used to initialize values to variables
    //  INPUT: id, exam1, exam2, final, prog, quizzes, addOrDelete
    //  PRE: all variables are assinged and ok
    //  OUTPUT: none
    //  POST: id, exam1, exam2, final, prog, quizzes, and addOrDelete are
    //  assinged
    //  NOTE: none
    {
      id = d;
      exam1 = g;
      exam2 = h;
      final = i;
      prog = j;
      quizzes = k;
      addOrDelete = p;
    }
    
    RelationType TestType::ComparedTo(TestType otherItem) const 
    //  PURPOSE: This comapres one item of ItemType to another item to return
    //  either greater, less, or equal
    //  INPUT: ItemType
    //  PRE: ItemType is valid and assinged
    //  OUTPUT: LESS, GREATER, or EQUAL
    //  POST: That less, greater, or equal is returned
    //  NOTE: none
    {						
      if (id < otherItem.id)
    	return  LESS;
      else if (id > otherItem.id)
    	return  GREATER;
      else  return  EQUAL;
    }
    
    char TestType::GetItemFromFile(ifstream& inFile)
    //  PURPOSE: This will read the items from an inFile, and will also
    //  determine what variables to read by it's first character
    //  INPUT: inFile
    //  PRE: inFile is opened and OK
    //  OUTPUT: addOrDelete
    //  POST: all variables are assinged and addOrDelete is returned
    //  NOTE: none
    {
      inFile >> addOrDelete;
      if (addOrDelete == 'A')
      {
         inFile >> id >> exam1 >> exam2 >> final >> prog  >> quizzes;
      }
      else if (addOrDelete == 'D')
      {
         inFile >> id;
      }
      return addOrDelete;
    }
      
    void TestType::WriteItemToFile(ofstream& outFile) const 
    //  PURPOSE: This will write all variables (id, exam1, exam2, final, prog,
    //  and quizzes, to an outFile
    //  INPUT: none
    //  PRE: outFile is opened and OK
    //  OUTPUT: outFile
    //  POST: all variables are assigned to the outFile
    //  NOTE: none
    {
      outFile.setf(ios::fixed);
      outFile.setf(ios::showpoint);
      outFile.precision(2);
      outFile << id << setw(12) << exam1 << setw(10) << exam2 << setw(10)
              << final << setw(10) << prog << setw(10) << quizzes << endl;
    }
    
    void TestType::PrintLstFullErr(ofstream& outFile) const
    //  PURPOSE: This will print the error message when the list is full and
    //  is trying to be added too
    //  INPUT: none
    //  PRE: list is full and trying to be written to
    //  OUTPUT: outFile
    //  POST: error message is written to outFile
    //  NOTE: none
    {
      outFile << "Error! The list is already full!!" << endl;
    }
    
    void TestType::PrintEmptyLstErr(ofstream& outFile)
    //  PURPOSE: This will print an error message when the list is empty but
    //  a line is trying to be deleted from it
    //  INPUT: none
    //  PRE: list is empty and is trying to be deleted from
    //  OUTPUT: outFile
    //  POST: error message is written to the outFile
    //  NOTE: none
    {
      outFile << "Error! You are trying to delete from an empty list!!"
              << endl;
    }
    
    SortedType.cxx
    
    #include "SortedLinkedList.h"
    
    SortedType::SortedType()  // Class constructor
    {
      length = 0;
      listData = NULL;
    }
    
    SortedType::~SortedType()
    // Post: List is empty; all items have been deallocated.
    {
      MakeEmpty();
    }
    
    SortedType::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* fromPtr;  // Pointer into list being copied from
        NodeType* toPtr;    // Pointer into new list being built
        if(otherList.listData == NULL)
        {
            listData = NULL;
            return;
        }
        // Copy first node
        fromPtr = otherList.listData;
        listData = new NodeType;
        listData->info = fromPtr->info;
    
        // Copy remaining nodes
    
        toPtr = listData;
        fromPtr = fromPtr->next;
        while (fromPtr != NULL)
        {
            toPtr->next = new NodeType;
            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;
      }
    } */
    
    bool SortedType::IsFull() const
    // Returns true if there is no room for another ItemType
    // object on the free store; false otherwise.
    {
      NodeType* ptr;
    
      ptr = new NodeType;
      if (ptr == NULL)
        return true;
      else
      {
        delete ptr;
        return false;
      }
    }
    
    int SortedType::LengthIs() const
    // Post: Number of items in the list is returned.
    {
      return length;
    }
    
    void SortedType::MakeEmpty()
    // Post: List is empty; all items have been deallocated.
    {
      NodeType*
      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
    }
    
    void SortedType::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* location = listData;
      NodeType* 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--;
    }
    
    void SortedType::ResetList()
    // Post: Current position has been initialized to AtEnd
    {
      currentPos = NULL;
    }
    
    void SortedType::RetrieveItem(ItemType& item, bool& found)
    {
      bool moreToSearch;
      NodeType* 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;
      }
    }
    
    void SortedType::InsertItem(ItemType item)
    {
      NodeType* newNode;  // pointer to node being inserted
      NodeType* predLoc;  // trailing pointer
      NodeType* 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;
      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++;
    }
    
    void SortedType::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;
    }
    
    void SortedType::Print()
    {
       currentPos = listData;
    
       while(currentPos != NULL)
       {
           cout << currentPos->info << " ";
           currentPos = currentPos->next;
       }
    }
    
    
    runSortedLinkedList
    
    #include "SortedLinkedList.h"
    #include <fstream>
    
    using namespace std;
    void FileToList(SortedType& 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
    
    void ListToFile(SortedType& 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 intList;
    
       FileToList(intList, inFile, outFile);
       ListToFile(intList, outFile);
    
       return 0;
    }
    
    void FileToList(SortedType& intList, ifstream& inFile, ofstream& 
    outFile)
    {
       TestType 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& intList, ofstream& outFile)
    {
       TestType 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);
       }
    }
    Thanks a lot guys.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    I need to be able to use ItemType throughout this whole program otherwise i cannot read or write to the outFile when needed. But the declaration of it twice is what would hang me up. ANY idea on how to write/re-write this? I am at a lost here and I have been starring at this for hours.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM