Cannot Declare Variable To Be of Abstract Type

May 1, 2012 at 12:57am
I'm just trying to see if everything that I have written so far is correct for my program. However whenever I try to compile, I receive there error message:
main.cpp:8: error: cannot declare variable ‘oList’ to be of abstract type ‘OListType<int>’


Below is my code. I can provide the derived abstract template class if need be as well.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "OListType.h"
#include "UListType.h"
#include <iostream>

using namespace std;

int main() {
  OListType<int> oList;
  UListType<int> uList;
  
  return 0;
}


OListType.h
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
#ifndef OListType_h
#define OListType_h

#include "ListType.h"

template <class T>
class OListType: public ListType<T> {
public:
  OListType(size_t=10);
  void insert(const T&);
  //void erase(const T&);
  //bool find(const T&) const;
};

template <class T>
OListType<T>::OListType(size_t n):ListType<T>(n){
}

template <class T>
void OListType<T>::insert(const T& item){
  if (this->count == this->capacity){
    this->capacity *= 2;
    T *temp = new T[this->capacity];
    for(int i=0; i<this->count; ++i)
      temp[i] = this->items[i];
    delete [] this->items;
    this->items = temp;
  }
  int i=this->count-1;
  while (i>=0 && item < this->items[i]) {
    this->items[i+1] = this->items[i];
    --i;
  }
  this->items[i+1] = item;
  ++this->count;
}

#endif 


UListType.h
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
#ifndef UListType_h
#define UListType_h

#include "ListType.h"

template <class T>
class UListType: public ListType<T> {
  public:
    UListType(size_t=10);
    //void insert(const T&);
    //void erase(const T&);
    bool find(const T&) const;
};

template <class T>
UListType<T>::UListType(size_t n):ListType<T>(n){
}

template <class T>
void UListType<T>::insert (const T& items) {
  if (this->count == this->capacity){
    this->capacity *= 2;
    T *temp = new T[this->capacity];
    for(int i=0; i<this->count; ++i)
      temp[i] = this->items[i];
    delete [] this->items;
    this->items = temp;
  }
  this->items[this->count] = items;
  ++this->count;
}

template <class T>
bool ListType<T>::find(const T& findItem) const {
  for (int i=0; i<this->count; ++i){
    if(findItem == items[i])
      return true;
  }
}
#endif 
Last edited on May 1, 2012 at 12:57am
May 1, 2012 at 1:00am
You can't create an instance of an abstract class. You have to keep a pointer or reference to it instead. What would happen if you called one of the abstract methods?
May 1, 2012 at 1:06am
Alright, so does that require me to change my whole code? I'm not quite sure I understand. I didn't think that OListType and UListType were abstract classes. I just thought they inherited from an abstract class.
May 1, 2012 at 1:08am
If you inherited from an abstract class, you need to implement all the virtual members in order to be able to define an instance of it. Make sure you have implemented all of the pure virtual methods from the base class in the derived class.
May 3, 2012 at 3:52am
Alright that was the problem. Thanks for your help!
Topic archived. No new replies allowed.