Hey guys, I was hoping if you could help me with this. I can do templates, and I can do inheritance, but when they're mixed up I get confused! :(
I keep getting compiler errors with these functions in my .cpp file:
template <class T>
class Container{
private:
int type; //the type number of the container.
public:
virtualvoid print()=0;
virtualvoid insert(T object)=0;
virtual T get() const =0;
virtual T& operator[](constint &num)=0;
Container(int type) { // constructor
this->type = type;
}
int gettype() const {
return type;
}
virtual ~Container() {};
};
template <class T>
class ArrayBaseContainer:public Container<T>{
private:
T* array; //save all the data
int inuse; //the number of spaces in use.
int size; //the size of the array
public:
/*
parameter:
@type:
@size: the initial size of the array
function:
initialize the array with the size and this->size is equal to the size.
*/
//constructor
ArrayBaseContainer(int type,int size);
~ArrayBaseContainer(); // destructor
//[] overloading
T& operator[](constint &num);
protected:
/*
parameter:
@object:
function:
add the object to the array.and increase the inused by 1.
*/
void insertObject(T object);
//return the max. size
int getSize();
//return the number of items inuse
int getInuse();
/*
parameter:
@newsize:
function:
re-allocate a new space with the size of newsize for the array
*/
void reallocate(int newsize);
public:
//print all the items in the array
void print();
//returns an item in the aray
T get() const;
};
**** Build of configuration Debug for project PA3 ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -ocontainer.o ..\container.cpp
..\container.cpp:6:1: error: 'ArrayBaseContainer' does not name a type
..\container.cpp:10:1: error: expected unqualified-id before 'template'
..\container.cpp:16:23: error: expected initializer before '<' token
..\container.cpp:22:23: error: expected initializer before '<' token
..\container.cpp:28:24: error: expected initializer before '<' token
..\container.cpp:33:24: error: expected initializer before '<' token
..\container.cpp:39:24: error: expected initializer before '<' token
..\container.cpp:44:21: error: expected initializer before '<' token
..\container.cpp:52:2: error: 'LinkedlistBaseContainer' does not name a type
..\container.cpp:56:25: error: expected constructor, destructor, or type conversion before '<' token
..\container.cpp:61:29: error: expected initializer before '<' token
..\container.cpp:66:29: error: expected initializer before '<' token
..\container.cpp:71:29: error: expected initializer before '<' token
..\container.cpp:76:26: error: expected initializer before '<' token
..\container.cpp:77:1: error: expected unqualified-id before '{' token
..\container.cpp:81:29: error: expected initializer before '<' token
..\container.cpp:86:28: error: expected initializer before '<' token
..\container.cpp:91:27: error: expected initializer before '<' token
..\container.cpp:100:1: error: 'StaticArray' does not name a type
..\container.cpp:104:1: error: expected unqualified-id before 'template'
..\container.cpp:114:1: error: 'DynamicArray' does not name a type
..\container.cpp:118:1: error: expected unqualified-id before 'template'
..\container.cpp:124:18: error: expected initializer before '<' token
..\container.cpp:133:1: error: 'Stack' does not name a type
..\container.cpp:137:1: error: expected unqualified-id before 'template'
..\container.cpp:143:11: error: expected initializer before '<' token
..\container.cpp:148:11: error: expected initializer before '<' token
..\container.cpp:157:1: error: 'Queue' does not name a type
..\container.cpp:161:1: error: expected unqualified-id before 'template'
..\container.cpp:167:11: error: expected initializer before '<' token
..\container.cpp:172:11: error: expected initializer before '<' token
Build error occurred, build is stopped
Time consumed: 178 ms.
Do I need to provide definitions for the Container class? But aren't they all virtual functions?
I would appreciate any help! Thanks so much!
template <class T>
int ArrayBaseContainer<T>::getSize()
{
return size;
}
template <class T>
int ArrayBaseContainer<T>::getInuse()
{
return inuse;
}
template <class T>
void ArrayBaseContainer<T>::insertObject(T object)
{
//let it be overridden?
}
template <class T>
void ArrayBaseContainer<T>::reallocate(int newsize)
{
//what do I put here? Do I just let it be overloaded?
}
template <class T>
void ArrayBaseContainer<T>::print()
{
}
template <class T>
T ArrayBaseContainer<T>::get() const
{
}
(There's more of it, but there's an error at every single definition)
Yup, I did that. Well, I also just compiled the main program, and got an entirely different set of errors. I just want to know if I'm doing anything glaringly wrong with this. :(