Templates and abstract classes
Jul 3, 2013 at 2:28am UTC
Hey guys, I'm getting an error from the compiler telling me that I have an abstract class but all the virtual functions are defined in it, not sure what is going on. Can someone help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
using namespace std;
template <class elemType>
class arrayListType
{
public :
const arrayListType<elemType>& operator =(const arrayListType<elemType>&);//
bool isEmpty() const ;//
bool isFull() const ;//
int listSize() const ;//
int maxListSize() const ;//
void print() const ;//
bool isItemAtEqual(int ,const elemType&) const ;//
virtual void insertAt(int location,const elemType& insertItem) = 0;//
virtual void insertEnd(const elemType& insertItem) = 0;//
void removeAt(int );//
void retrieveAt(int ,elemType&) const ;//
virtual void replaceAt(int location, const elemType& repItem) = 0;//
void clearList();//
virtual int seqSearch(const elemType& searchItem) = 0;//
virtual void remove(const elemType& removeItem) = 0;//
arrayListType(int size = 100);//
arrayListType(const arrayListType<elemType>& otherList);//
virtual ~arrayListType();//
protected :
elemType *list;
int length;
int maxSize;
};
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " " ;
cout << endl;
}
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int location, const elemType& item) const
{
if (location < 0 || location >= length)
{
cout << "Location is out of range. " << endl;
return false ;
}
else
return (list[location] == item);
}
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cout << "Location is out of range." << endl;
else
{
for (int i = location; i < length; i++)
list[i] = list[i + 1];
length--;
}
}
template <class elemType>
void arrayListType<elemType>::retrieveAt(int location, elemType& retItem) const
{
if (location < 0 || location >= length)
cout << "Location is oit of range." << endl;
else
retItem = list[location];
}
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size <= 0)
{
cout << "Size must be positive, creating default size of 100." << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
}
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator =(const arrayListType<elemType>& otherList)
{
if (this != &otherList)
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this ;
}
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
}
template <class elemType>
arrayListType<elemType>::arrayListType(const arrayListType<elemType> &otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#ifndef H_unorderedArrayListType
#define H_unorderedArrayListType
#include "arrayListType.h"
#include <iostream>
using namespace std;
template <class elemType>
class unorderedArrayListType: public arrayListType<elemType>
{
public :
void insertAt(int location,const elemType& insertItem);//
void insertEnd(const elemType& insertItem);//
void replaceAt(int location,const elemType& repItem);//
int seqSearch(const elemType& searchItem) const ;//
void remove(const elemType& removeItem);//
unorderedArrayListType(int size = 100);//
};
template <class elemType>
void unorderedArrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if (length >= maxSize)
cout << "The list is full. " << endl;
else
{
list[length] = insertItem;
length++;
}
}
template <class elemType>
int unorderedArrayListType<elemType>::seqSearch(const elemType& searchItem) const
{
int loc;
bool found = false ;
for (loc = 0; loc < length; loc++)
if (list[loc] == searchItem)
{
found = true ;
break ;
}
if (found)
return loc;
else
return -1;
}
template <class elemType>
void unorderedArrayListType<elemType>::remove(const elemType& removeItem)
{
int loc;
if (length == 0)
cout << "The list is empty. " << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item is not in the list. " << endl;
}
}
template <class elemType>
void unorderedArrayListType<elemType>::replaceAt(int location, const elemType& repItem)
{
if (location < 0 || location >= length)
cout << "Location is out of range. " << endl;
else
list[location] = repItem;
}
template <class elemType>
void unorderedArrayListType<elemType>::insertAt(int location, const elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cout << "Location is out of range." << endl;
else if (length >= maxSize)
cout << "The list is full. " << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1];
list[location] = insertItem;
length++;
}
}
template <class elemType>
unorderedArrayListType<elemType>::unorderedArrayListType(int size)
:arrayListType<elemType>(size)
{
}
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
#include "unorderedArrayListType.h"
using namespace std;
int main()
{
unorderedArrayListType<string> strList(25);
unorderedArrayListType<int > intList(25);
system("PAUSE" );
return 0;
}
1>c:\users\joseph\documents\visual studio 2008\projects\moreexamples\moreexamples\main.cpp(10) : error C2259: 'unorderedArrayListType<elemType>' : cannot instantiate abstract class
1> with
1> [
1> elemType=std::string
1> ]
1> due to following members:
1> 'int arrayListType<elemType>::seqSearch(const elemType &)' : is abstract
1> with
1> [
1> elemType=std::string
1> ]
1> c:\users\joseph\documents\visual studio 2008\projects\moreexamples\moreexamples\arraylisttype.h(25) : see declaration of 'arrayListType<elemType>::seqSearch'
1> with
1> [
1> elemType=std::string
1> ]
1>c:\users\joseph\documents\visual studio 2008\projects\moreexamples\moreexamples\main.cpp(11) : error C2259: 'unorderedArrayListType<elemType>' : cannot instantiate abstract class
1> with
1> [
1> elemType=int
1> ]
1> due to following members:
1> 'int arrayListType<elemType>::seqSearch(const elemType &)' : is abstract
1> with
1> [
1> elemType=int
1> ]
1> c:\users\joseph\documents\visual studio 2008\projects\moreexamples\moreexamples\arraylisttype.h(25) : see declaration of 'arrayListType<elemType>::seqSearch'
1> with
1> [
1> elemType=int
1> ]
1>Build log was saved at "file://c:\Users\Joseph\Documents\Visual Studio 2008\Projects\MoreExamples\MoreExamples\Debug\BuildLog.htm"
Jul 3, 2013 at 2:35am UTC
You need to redefine ALL the pure virtual functions in the derived class, otherwise the class is incomplete - that is still abstract, because the all the functions are inherited.
HTH
Jul 3, 2013 at 2:53am UTC
The pure virtual functions are omitted from the base class and defined in the derived class. Would you mind giving me an example of what I need to do?
Topic archived. No new replies allowed.