Cannot instantiate abstract class

Before posting this forum, I have tried to search through the forum but I still cannot find the solution and I don't understand...

So I decide to post here and have you guys help!

Thank you in advance :))

//////////////////////////////////////////////main.cpp/////////

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "ListBase.h"
#include "circularLL.h"


int main()
{    ListBase* intList;
    int rItem;

    intList = new CircularLL();   <-- error here : cannot instantiate abstract class
return 0;
}

///////////////////////////////////////// ListBase.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
#ifndef LISTBASE_H
#define LISTBASE_H

#include<iostream>

class ListBase{
protected:
	int _size;

public:
	ListBase() {_size = 0;}
	virtual ~ListBase();

	bool isEmpty() {return _size==0; };
	int getLength() {return _size; };

	virtual bool insert(int index, const int &newItem) = 0;
	virtual bool remove(int index) = 0;
	virtual bool retrieve(int index, const int &dataItem) = 0;

	virtual std::string toString() = 0;


};


#endif 

//////////////////////////////////////////////CircularLL.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
#ifndef CIRCULARLL_H
#define CIRCULARLL_H

#include"ListBase.h"

class CircularLL: public ListBase {
private:
	struct Listnode{
		int item;
		Listnode *next;
	};

	Listnode *_tail;

	Listnode* traverseTo(int index);

public:
	CircularLL();
	~CircularLL();

	bool insert(int index, const int &newItem);
	bool remove(int index);
	bool retrieve(int index, int &dataItem);

	std::string toString();

};




#endif 



///////////////////////////////////////////////CircularLL.cpp///////////
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
#include<iostream>
#include<sstream>
#include"circularLL.h"


CircularLL::Listnode* CircularLL::traverseTo(int index) {
	Listnode *cur;
	int i=0;
	cur = _tail->next;
	for(i=0; i<index; i++)
		cur = cur->next;
	return cur;
}

CircularLL::~CircularLL() {
	while(!isEmpty())
		remove(0);
}

CircularLL::CircularLL(void) 
	: ListBase(), _tail(NULL) {}

bool CircularLL::insert(int index, const int &newItem){
	if(_size == 0) {
		_tail = new Listnode;
		_tail->next = _tail;
	}
	else {
		Listnode* cur = traverseTo(index -1);
		Listnode* temp = new Listnode;
		temp->item = newItem;
		temp->next = cur->next;
		cur->next = temp;
	}
	_size++;
	return 0;
}

bool CircularLL::remove(int index) {
	Listnode *cur = traverseTo(index -1);
	if(_size == 0)
		return false;
	else {
		cur->next = cur->next->next;
		delete cur->next;
		_size --;
		return true;
	}
}

bool CircularLL::retrieve(int index, int &dataItem) {
	Listnode *cur = traverseTo(index);
	dataItem = cur->item;
	return true;
}

std::string CircularLL::toString() {
	std::ostringstream os;
	os <<  "[ ";
	for(int i=0; i<_size; i++){
		_tail = _tail->next;
		os << _tail->item << " ";
	}
	os << "]";
	return os.str();
}

/////////////////////////////////////////////////////////////////

thank you again :)
Last edited on
bool retrieve(int index, int &dataItem); (CircularLL.h:23)

should be

bool retrieve(int index, const int &dataItem);

Otherwise you're not actually overriding the virtual function from the base class, making CircularLL an abstract class.

Do the same with the definition (CircularLL.cpp:51)
Last edited on
Thank you very much now this error is solved...

but there is a new error

Error 2 error LNK1120: 1 unresolved externals
File:C:\Users\R_____\Documents\COURSE\CG1103\tutorial\circularlinkedlist\Debug\circularlinkedlist.exe
Line:1

Error 1 error LNK2019: unresolved external symbol "public: virtual __thiscall ListBase::~ListBase(void)" (??1ListBase@@UAE@XZ) referenced in function "public: virtual __thiscall CircularLL::~CircularLL(void)" (??1CircularLL@@UAE@XZ)
File: C:\Users\R____\Documents\COURSE\CG1103\tutorial\circularlinkedlist\circularlinkedlist\CircularLL.obj
You haven't provided a body for ListBase's destructor.

1
2
3
4
5
6
7
8
class ListBase{
protected:
	int _size;

public:
	ListBase() {_size = 0;}
	virtual ~ListBase() {}; //add body here
...
Topic archived. No new replies allowed.