Getting error C2259 "cannot instantiate abstract class"

Ok this is so bloody simple that it hurts me that I’m asking. I’m getting "error C2259: 'AdapterStack<T>' : cannot instantiate abstract class"

The three files are:

StackTest.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>// needed or cin, cout, endl are undefined
//#include "Stack.h"
#include "AdapterStack.h"
using namespace std;

using namespace std;

void main()
{
	AdapterStack<string> stack1; //This is the line erroring out
}


AdapterStack.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 _ADAPTERSTACK_H
#define _ADAPTERSTACK_H
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Stack.h"
using namespace std;

template <typename T>
class AdapterStack : public Stack<T> {

public:
	AdapterStack();
	~AdapterStack();
	bool empty() const;
	T push(T item);
	T pop();
	//T peek() const; //this line was causing another error too but its comment out at the moment.

};

template <typename T>
AdapterStack<T>::AdapterStack(){
}

#endif 


Stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _STACK_H
#define _STACK_H

#include <iostream>
using namespace std;

template <typename T>
class Stack{
public:

	virtual bool empty() const =0;
	virtual T push(T item) = 0;
	virtual T pop() = 0;
	virtual T peek() const = 0;
	//virtual void print (std::ostream& out) const = 0;
	
};
#endif 


That’s it. There isn’t anymore to this program at the moment. I know its something fundamental but none of my reference books are helping and I can’t find a good example online. What I’m having a hard time rapping my brain around is having a class be both Abstract and a Template. I fairly positive that the fact Stack.h is a template little to do with this but I’m weak enough in the subject that I’m second guessing everything.

Thanks for any help I can get.
First, use int main().

You have a pure virtual function in your Stack class that isn't defined in your AdapterStack class. (peek)
Ok so I fixed some of the obvious. The issue I was having with the peek() function was that I didn’t have any of the method defined. So now they all are but now I’m getting “error C4430: missing type specifier - int assumed. Note: C++ does not support default-int” I get one for each method.

AdapterStack.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef _ADAPTERSTACK_H
#define _ADAPTERSTACK_H

#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Stack.h"
using namespace std;



template <typename T>
class AdapterStack : public Stack<T> {

public:
	AdapterStack();
	~AdapterStack();

	bool empty() const;
	T push(T item);
	T pop();
	T peek() const;

private:
	vector<T*> vecStack;
};

template <typename T>
AdapterStack<T>::AdapterStack(){

}

template <typename T>
AdapterStack<T>::~AdapterStack(){

}

template <typename T>
AdapterStack<T>::empty() const{
	return vecStack.empty();
}

template <typename T>
AdapterStack<T>::peek() const{
	vecStack.back();
}

template <typename T>
AdapterStack<T>::pop(){
	vecStack.pop_back();
}

template <typename T>
AdapterStack<T>::push(T item){
	vecStack.push_back(item);
}

#endif 


Do your methods return anything?
Alright I made so the method that don't need to return anything but now the push(T item) method is freaking out.

error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'std::string' to 'std::basic_string<_Elem,_Traits,_Ax> &'


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
#ifndef _ADAPTERSTACK_H
#define _ADAPTERSTACK_H

#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Stack.h"
using namespace std;



template <typename T>
class AdapterStack : public Stack<T> {

public:
	AdapterStack();
	~AdapterStack();

	bool empty() const;
	void push(T item);
	void pop();
	T peek() const;

private:
	vector<T*> vecStack;
};

template <typename T>
AdapterStack<T>::AdapterStack(){

}

template <typename T>
AdapterStack<T>::~AdapterStack(){

}

template <typename T>
bool AdapterStack<T>::empty() const{
	return vecStack.empty();
}

template <typename T>
T AdapterStack<T>::peek() const{
	vecStack.back();
}

template <typename T>
void AdapterStack<T>::pop(){
	vecStack.pop_back();
}

template <typename T>
void AdapterStack<T>::push(T item){
	vecStack.push_back(item);
}

#endif 


Thansk for all the help I gotten so far.
vecStack is of type std::vector<T *>, and you're trying to push a T. I don't understand why the error reports the type as 'std::string &', though.
In addition, vecStack should be initialized in the ctor (lines 29 to 32).
Thanks, I got it worked out.
Topic archived. No new replies allowed.