problem with templates and inheritance

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:

(mostly empty for now)
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
template<class T>
ArrayBaseContainer<T>::ArrayBaseContainer(int type, int size)
{
	
}

template<class T>
ArrayBaseContainer<T>::~ArrayBaseContainer()
{
	//nothing, use the destructors for static and dynamic
}

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)
{
}

template <class T>
void ArrayBaseContainer<T>::reallocate(int newsize)
{
}

template <class T>
void ArrayBaseContainer<T>::print()
{
}						

template <class T>
T ArrayBaseContainer<T>::get() const
{
}


Here is my .h file.
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
template <class T>
class Container{
 private:
  int type; //the type number of the container.
 public:
  virtual void print()=0; 
  virtual void insert(T object)=0;
  virtual T  get() const =0;
  virtual T& operator[](const int &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[](const int &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;	
};


The errors look like this:
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
**** 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!
I think I figured it out, somehow. I'm not supposed to have constructors in there, it seems...
Okay, what?!
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
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)


The error:
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
**** 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:17:23: error: expected initializer before '<' token
..\container.cpp:23:23: error: expected initializer before '<' token
..\container.cpp:29:24: error: expected initializer before '<' token
..\container.cpp:35:24: error: expected initializer before '<' token
..\container.cpp:41:24: error: expected initializer before '<' token
..\container.cpp:46:21: error: expected initializer before '<' token
..\container.cpp:64:29: error: expected initializer before '<' token
..\container.cpp:69:29: error: expected initializer before '<' token
..\container.cpp:74:29: error: expected initializer before '<' token
..\container.cpp:79:26: error: expected initializer before '<' token
..\container.cpp:84:29: error: expected initializer before '<' token
..\container.cpp:89:28: error: expected initializer before '<' token
..\container.cpp:94:27: error: expected initializer before '<' token
..\container.cpp:108:17: error: expected initializer before '<' token
..\container.cpp:128:18: error: expected initializer before '<' token
..\container.cpp:142:11: error: expected initializer before '<' token
..\container.cpp:147:11: error: expected initializer before '<' token
..\container.cpp:152:11: error: expected initializer before '<' token
..\container.cpp:166:11: error: expected initializer before '<' token
..\container.cpp:171:11: error: expected initializer before '<' token
..\container.cpp:176:11: error: expected initializer before '<' token
Build error occurred, build is stopped
Time consumed: 138  ms.  
Did you try to include your cpp at the then of your class definition?
arraybase.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef ARRAY_BASE_H
#define ARRAY_BASE_H

#include "container.h"
template <class T>
class ArrayBaseContainer:public Container<T>{
//...
};

#include "arraybase.hpp"
#endif 

arraybase.hpp
1
2
3
#include "arraybase.h" //to make it look nice
#include "container.h"
//all the definitions 

main.cpp
1
2
#include "arraybase.h"
//... 


And compile just main
g++ main.cpp
Last edited on
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. :(

http://dl.dropbox.com/u/9365368/errors.jpg
Last edited on
Topic archived. No new replies allowed.