class and object (.cpp file accessing .h file) - BUG!

I get a weird bug: "error: cannot declare variable ‘use’ to be of abstract type ‘test’", any idea where it is coming from ?



1
2
3
4
5
6
7
8
9
10
11

// Note: test is the name of class inside test.h file and use is the object

a.cpp: In function ‘int main()’:
a.cpp:20:17: error: cannot declare variable ‘use’ to be of abstract type ‘test’
   A use("q1");   
                 ^
compilation terminated due to -Wfatal-errors.
<builtin>: recipe for target 'a' failed
make: *** [a] Error 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// a.cpp file

#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include "Q.h"
#include "test.h"

using namespace std;

	int main(){
// Note: test is the name of class inside test.h file and use is the object
		string q1 = "How are you?";
		test use(q1);
		use.print();

		return 0;
	}



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
//test.h file

#ifndef test_h
#define test_h
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include "Q.h"		// base .h file
using namespace std;

//Note: a is a namespace, and Q is the class inside the namespace in Q.h file
	class test : public a::Q {		
		private:
			vector <string> q;
		public:

			//default constructor: 
			test(string x)
			{
				q.push_back(x);
			}

			// returns the questions to print function
			string return_text() const{
				for (int i = 0; i < q.size(); ++i)
				{
					string pass = q[i];
					return pass;
				}
			}

			void print() const{

				string print = return_text();
				cout << print << endl;

			}

			~test(){}
	};

#endif 


Last edited on
could be that class Q in namespace a is abstract with at least one pure virtual function for which you haven't provided an override in class test
Last edited on
@gunnerfunner, yes you are right. I didn't know how to write the code for one of the functions in test.h, so I comment it out, but virtual function itself existed inside Q.h, so I had to comment that as well. I did it and it worked!

Thanks alot my friend.
rather than say 'override the base class pure virtual function within the derived class' it might be better to say 'provide an implementation for the base class pure virtual function within the derived class' as this function is not implemented in the base class anyways
Topic archived. No new replies allowed.