By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,020 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,020 IT Pros & Developers. It's quick & easy.

class templates

P: n/a
#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>::DeleteItem(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?

Mar 27 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ok. Sorry members. I figured out some of my syntax errors related to
the MakeEmpty function etc. I have corrected those and I am still
getting an error regarding that RelationType declaration. Is it a good
idea to have 3 separate files since class templates I am told
eliminates the need for more files.
I have declared this is a .h file and I have my main in a driver file
and the above class template in a .cpp file.

#include <fstream>

enum RelationType {LESS,GREATER,EQUAL};

class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Initialize(int number);
private:
int value;
};

e included from template.cpp:4,
from templatedriver.cpp:4:
template.h:3: error: multiple definition of `enum RelationType'
template.h:3: error: previous definition here
template.h:3: error: conflicting declaration 'LESS'
template.h:3: error: 'LESS' has a previous declaration as
`RelationType LESS'
template.h:3: error: declaration of `LESS'
template.h:3: error: conflicts with previous declaration `RelationType
LESS'
template.h:3: error: conflicting declaration 'GREATER'
template.h:3: error: 'GREATER' has a previous declaration as
`RelationType GREATER'
template.h:3: error: declaration of `GREATER'
template.h:3: error: conflicts with previous declaration `RelationType
GREATER'
template.h:3: error: conflicting declaration 'EQUAL'
template.h:3: error: 'EQUAL' has a previous declaration as
`RelationType EQUAL'
template.h:3: error: declaration of `EQUAL'
template.h:3: error: conflicts with previous declaration `RelationType
EQUAL'
template.h:6: error: redefinition of `class ItemType'
template.h:6: error: previous definition of `class ItemType'

templatedriver.cpp: In function `int main()':
templatedriver.cpp:31: error: `intData' undeclared (first use this
function)
templatedriver.cpp:31: error: (Each undeclared identifier is reported
only once for each function it appears in.)

templatedriver.cpp:31: error: `tmp' undeclared (first use this
function)
templatedriver.cpp:32: error: missing template arguments before ','
token
templatedriver.cpp:32: error: expected primary-expression before
"bool"
templatedriver.cpp:33: error: `j' undeclared (first use this function)

make.exe: *** [templatedriver.o] Error 1

Execution terminated

Mar 27 '07 #2

P: n/a
zf*****@umd.umich.edu wrote:
....
e included from template.cpp:4,
from templatedriver.cpp:4:
template.h:3: error: multiple definition of `enum RelationType'
template.h:3: error: previous definition here
What do you think the errors above mean ?
Mar 27 '07 #3

P: n/a
On Mar 27, 10:45 pm, zfar...@umd.umich.edu wrote:
Ok. Sorry members. I figured out some of my syntax errors related to
the MakeEmpty function etc. I have corrected those and I am still
getting an error regarding that RelationType declaration. Is it a good
idea to have 3 separate files since class templates I am told
eliminates the need for more files.
I have declared this is a .h file and I have my main in a driver file
and the above class template in a .cpp file.

#include <fstream>

enum RelationType {LESS,GREATER,EQUAL};

class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Initialize(int number);
private:
int value;

};

e included from template.cpp:4,
from templatedriver.cpp:4:
template.h:3: error: multiple definition of `enum RelationType'
template.h:3: error: previous definition here
template.h:3: error: conflicting declaration 'LESS'
template.h:3: error: 'LESS' has a previous declaration as
`RelationType LESS'
template.h:3: error: declaration of `LESS'
template.h:3: error: conflicts with previous declaration `RelationType
LESS'
template.h:3: error: conflicting declaration 'GREATER'
template.h:3: error: 'GREATER' has a previous declaration as
`RelationType GREATER'
template.h:3: error: declaration of `GREATER'
template.h:3: error: conflicts with previous declaration `RelationType
GREATER'
template.h:3: error: conflicting declaration 'EQUAL'
template.h:3: error: 'EQUAL' has a previous declaration as
`RelationType EQUAL'
template.h:3: error: declaration of `EQUAL'
template.h:3: error: conflicts with previous declaration `RelationType
EQUAL'
template.h:6: error: redefinition of `class ItemType'
template.h:6: error: previous definition of `class ItemType'

templatedriver.cpp: In function `int main()':
templatedriver.cpp:31: error: `intData' undeclared (first use this
function)
templatedriver.cpp:31: error: (Each undeclared identifier is reported
only once for each function it appears in.)

templatedriver.cpp:31: error: `tmp' undeclared (first use this
function)
templatedriver.cpp:32: error: missing template arguments before ','
token
templatedriver.cpp:32: error: expected primary-expression before
"bool"
templatedriver.cpp:33: error: `j' undeclared (first use this function)

make.exe: *** [templatedriver.o] Error 1

Execution terminated
It seems that multiple headers include the one with definition of
RelationType and ItemType.
It is advisable to enclose the header file in include guards, e.g. for
header.h add at the beginning:
#ifndef HEADERH_NCLUDED
#define HEADERH_INCLUDED
.....
contents of header
.....
#endif

now if header.h is included twice during compilation of single cpp
file, the classes/enums defined
inside will be defined only once.

Mar 27 '07 #4

P: n/a
zf*****@umd.umich.edu wrote:
Ok. Sorry members. I figured out some of my syntax errors related to
the MakeEmpty function etc. I have corrected those and I am still
getting an error regarding that RelationType declaration.

#include <fstream>

enum RelationType {LESS,GREATER,EQUAL};
....
e included from template.cpp:4,
from templatedriver.cpp:4:
template.h:3: error: multiple definition of `enum RelationType'
template.h:3: error: previous definition here
google: include guard
Mar 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.