![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 1
Rep Power: 0
![]() |
non-aggregate type:errors
Please help me with this code... It is a bit long, but please bear with me:
ItemType.cxx
RelationType ItemType::ComparedTo(ItemType 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 ItemType::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 ItemType::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 ItemType::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 ItemType::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;
}
SortedLinkedList.h
#include "ItemType.h"
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
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>
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;
}
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>::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++;
}
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<ItemType> intList;
FileToList(intList, inFile, outFile);
ListToFile(intList, outFile);
return 0;
}
template<class ItemType>
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);
}
}
}
template <class ItemType>
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);
}
}Here are my error messages: runSortedLinkedList.cxx: In function `void FileToList(SortedType<ItemType>&, std::ifstream&, std::ofstream&) [with ItemType = int]': runSortedLinkedList.cxx:50: instantiated from here runSortedLinkedList.cxx:65: error: request for member `GetItemFromFile' in ` item', which is of non-aggregate type `int' runSortedLinkedList.cxx:50: instantiated from here runSortedLinkedList.cxx:74: error: request for member `PrintLstFullErr' in ` item', which is of non-aggregate type `int' runSortedLinkedList.cxx:76: error: request for member `GetItemFromFile' in ` item', which is of non-aggregate type `int' runSortedLinkedList.cxx:50: instantiated from here runSortedLinkedList.cxx:82: error: request for member `PrintEmptyLstErr' in ` item', which is of non-aggregate type `int' runSortedLinkedList.cxx:50: instantiated from here runSortedLinkedList.cxx:88: 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:109: error: request for member `WriteItemToFile' in ` item', which is of non-aggregate type `int' I have no idea how to fix this error, and any help form anyone would be greatly appreciated. If you know what code I can change or put in let me know because I am stuck here. Thank you! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|