Z
zfareed
#include <iostream>
#include <fstream>
using namespace std;
template<class ItemType>
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
SortedList( ); // default constructor: lenght=0,
currentPos=-1
void MakeEmpty; // let length=0
void InsertItem(ItemType x); // insert x into the list
void DeleteItem(ItemType x); // delete x from the list
bool IsFull( ); // test if the list is full
int Lengthls( ); // return length
void RetrieveItem(ItemType &x, bool &found); // retrieve
x from the list, the
// boolean result is stored in found
void ResetList( ); // currentPos=-1
void GetNextItem(ItemType &x); // get the next element
from the list with
// respect to the currentPos
};
template <class ItemType>
SortedList<ItemType>::SortedList()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void SortedList<ItemType>::MakeEmpty()
{
length =0;
}
template <class ItemType>
void SortedList<ItemType>::InsertItem(ItemType x)
{
bool moreToSearch;
int location = 0;
moreToSearch = (location < length);
while(moreToSearch)
{
switch(x.ComparedTo(values[location]))
{
case LESS : moreToSearch = false;
break;
case GREATER : location++;
moreToSearch= (location < length);
break;
}
}
}
template <class ItemType>
void SortedList<ItemType>:eleteItem(ItemType x)
{
int location = 0;
while(x.ComparedTo(values[location]) != EQUAL)
location++;
for(int index = location + 1;index < length; index++)
values[index - 1] = values[index];
length--;
}
template <class ItemType>
bool SortedList<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int SortedList<ItemType>::LengthIs()
{
return length;
}
template<class ItemType>
void SortedList<ItemType>::RetrieveItem(ItemType &x,bool &found)
{
int midPoint;
int first = 0;
int last = length - 1;
bool moreToSearch = first <= last;
found = false;
while(moreToSearch && !found)
{
midPoint = (first + last) / 2;
switch(x.ComparedTo(values[midPoint]))
{
case LESS : last = midPoint - 1;
moreToSearch = first <= last;
break;
case GREATER : first = midPoint - 1;
moreToSearch = first <= last;
break;
case EQUAL : found = true;
x = values[midPoint];
break;
}
}
}
template <class ItemType>
void SortedList<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void SortedList<ItemType>::GetNextItem(ItemType &x)
{
currentPos++;
x = values[currentPos];
}
void CreateListFromFile(std::ifstream& dataFile, ItemType& list)
{
ItemType x;
list.MakeEmpty();
GetData(dataFile, x);
while(dataFile)
{
if(!list.IsFull())
list.InsertItem(item);
GetData(dataFile.item);
}
}
**********************************************************************************************
Q1. I am getting the foll. compiler errors:
template.cpp:18: error: variable or field `MakeEmpty' declared void
template.cpp:40: error: no `void SortedList<ItemType>::MakeEmpty()'
member function declared in class `SortedList<ItemType>'
template.cpp:40: error: template definition of non-template `void
SortedList<ItemType>::MakeEmpty()'
template.cpp: In member function `void
SortedList<ItemType>::InsertItem(ItemType)':
template.cpp:55: error: `LESS' undeclared (first use this function)
template.cpp:55: error: (Each undeclared identifier is reported only
once for each function it appears in.)
template.cpp:57: error: `GREATER' undeclared (first use this function)
template.cpp: At global scope:
template.cpp:84: error: no `int SortedList<ItemType>::LengthIs()'
member function declared in class `SortedList<ItemType>'
template.cpp:84: error: template definition of non-template `int
SortedList<ItemType>::LengthIs()'
template.cpp:123: error: no `void SortedList<ItemType>::ResetList()'
member function declared in class `SortedList<ItemType>'
template.cpp:123: error: template definition of non-template `void
SortedList<ItemType>::ResetList()'
template.cpp:131: error: no `void
SortedList<ItemType>::GetNextItem(ItemType&)' member function declared
in class `SortedList<ItemType>'
template.cpp:131: error: template definition of non-template `void
SortedList<ItemType>::GetNextItem(ItemType&)'
template.cpp:137: error: `ItemType' has not been declared
template.cpp:138: error: ISO C++ forbids declaration of `list' with no
type
template.cpp: In function `void CreateListFromFile(std::ifstream&,
int&)':
template.cpp:139: error: `ItemType' undeclared (first use this
function)
template.cpp:139: error: expected `;' before "x"
template.cpp:140: error: `MakeEmpty' has not been declared
template.cpp:140: error: request for member of non-aggregate type
before '(' token
template.cpp:141: error: `x' undeclared (first use this function)
template.cpp:141: error: `GetData' undeclared (first use this
function)
template.cpp:144: error: `IsFull' has not been declared
template.cpp:144: error: request for member of non-aggregate type
before '(' token
template.cpp:145: error: `InsertItem' has not been declared
template.cpp:145: error: request for member of non-aggregate type
before '(' token
template.cpp:145: error: `item' undeclared (first use this function)
template.cpp:146: error: 'struct std::basic_ifstream<char,
std::char_traits<char> >' has no member named 'item'
make.exe: *** [template.o] Error 1
Execution terminated
I am trying to implement the member functions beneath the class
template and input values from a file and into an array based list.
Why are the functions not being recognised?
****************************************************************************************************************************
#include <fstream>
enum RelationType {LESS,GREATER,EQUAL};
class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Initialize(int number);
private:
int value;
};
Q2. When I declare this in a header file to define the ComparedTo
Function I get an error that says it is redundant. What does that mean?
#include <fstream>
using namespace std;
template<class ItemType>
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
SortedList( ); // default constructor: lenght=0,
currentPos=-1
void MakeEmpty; // let length=0
void InsertItem(ItemType x); // insert x into the list
void DeleteItem(ItemType x); // delete x from the list
bool IsFull( ); // test if the list is full
int Lengthls( ); // return length
void RetrieveItem(ItemType &x, bool &found); // retrieve
x from the list, the
// boolean result is stored in found
void ResetList( ); // currentPos=-1
void GetNextItem(ItemType &x); // get the next element
from the list with
// respect to the currentPos
};
template <class ItemType>
SortedList<ItemType>::SortedList()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void SortedList<ItemType>::MakeEmpty()
{
length =0;
}
template <class ItemType>
void SortedList<ItemType>::InsertItem(ItemType x)
{
bool moreToSearch;
int location = 0;
moreToSearch = (location < length);
while(moreToSearch)
{
switch(x.ComparedTo(values[location]))
{
case LESS : moreToSearch = false;
break;
case GREATER : location++;
moreToSearch= (location < length);
break;
}
}
}
template <class ItemType>
void SortedList<ItemType>:eleteItem(ItemType x)
{
int location = 0;
while(x.ComparedTo(values[location]) != EQUAL)
location++;
for(int index = location + 1;index < length; index++)
values[index - 1] = values[index];
length--;
}
template <class ItemType>
bool SortedList<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int SortedList<ItemType>::LengthIs()
{
return length;
}
template<class ItemType>
void SortedList<ItemType>::RetrieveItem(ItemType &x,bool &found)
{
int midPoint;
int first = 0;
int last = length - 1;
bool moreToSearch = first <= last;
found = false;
while(moreToSearch && !found)
{
midPoint = (first + last) / 2;
switch(x.ComparedTo(values[midPoint]))
{
case LESS : last = midPoint - 1;
moreToSearch = first <= last;
break;
case GREATER : first = midPoint - 1;
moreToSearch = first <= last;
break;
case EQUAL : found = true;
x = values[midPoint];
break;
}
}
}
template <class ItemType>
void SortedList<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void SortedList<ItemType>::GetNextItem(ItemType &x)
{
currentPos++;
x = values[currentPos];
}
void CreateListFromFile(std::ifstream& dataFile, ItemType& list)
{
ItemType x;
list.MakeEmpty();
GetData(dataFile, x);
while(dataFile)
{
if(!list.IsFull())
list.InsertItem(item);
GetData(dataFile.item);
}
}
**********************************************************************************************
Q1. I am getting the foll. compiler errors:
template.cpp:18: error: variable or field `MakeEmpty' declared void
template.cpp:40: error: no `void SortedList<ItemType>::MakeEmpty()'
member function declared in class `SortedList<ItemType>'
template.cpp:40: error: template definition of non-template `void
SortedList<ItemType>::MakeEmpty()'
template.cpp: In member function `void
SortedList<ItemType>::InsertItem(ItemType)':
template.cpp:55: error: `LESS' undeclared (first use this function)
template.cpp:55: error: (Each undeclared identifier is reported only
once for each function it appears in.)
template.cpp:57: error: `GREATER' undeclared (first use this function)
template.cpp: At global scope:
template.cpp:84: error: no `int SortedList<ItemType>::LengthIs()'
member function declared in class `SortedList<ItemType>'
template.cpp:84: error: template definition of non-template `int
SortedList<ItemType>::LengthIs()'
template.cpp:123: error: no `void SortedList<ItemType>::ResetList()'
member function declared in class `SortedList<ItemType>'
template.cpp:123: error: template definition of non-template `void
SortedList<ItemType>::ResetList()'
template.cpp:131: error: no `void
SortedList<ItemType>::GetNextItem(ItemType&)' member function declared
in class `SortedList<ItemType>'
template.cpp:131: error: template definition of non-template `void
SortedList<ItemType>::GetNextItem(ItemType&)'
template.cpp:137: error: `ItemType' has not been declared
template.cpp:138: error: ISO C++ forbids declaration of `list' with no
type
template.cpp: In function `void CreateListFromFile(std::ifstream&,
int&)':
template.cpp:139: error: `ItemType' undeclared (first use this
function)
template.cpp:139: error: expected `;' before "x"
template.cpp:140: error: `MakeEmpty' has not been declared
template.cpp:140: error: request for member of non-aggregate type
before '(' token
template.cpp:141: error: `x' undeclared (first use this function)
template.cpp:141: error: `GetData' undeclared (first use this
function)
template.cpp:144: error: `IsFull' has not been declared
template.cpp:144: error: request for member of non-aggregate type
before '(' token
template.cpp:145: error: `InsertItem' has not been declared
template.cpp:145: error: request for member of non-aggregate type
before '(' token
template.cpp:145: error: `item' undeclared (first use this function)
template.cpp:146: error: 'struct std::basic_ifstream<char,
std::char_traits<char> >' has no member named 'item'
make.exe: *** [template.o] Error 1
Execution terminated
I am trying to implement the member functions beneath the class
template and input values from a file and into an array based list.
Why are the functions not being recognised?
****************************************************************************************************************************
#include <fstream>
enum RelationType {LESS,GREATER,EQUAL};
class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Initialize(int number);
private:
int value;
};
Q2. When I declare this in a header file to define the ComparedTo
Function I get an error that says it is redundant. What does that mean?