Problem with C++ and derived class

brazoayeye
Posts: 41
Joined: Sun May 03, 2020 2:37 pm

Problem with C++ and derived class

Postby brazoayeye » Fri Aug 04, 2023 2:48 pm

Hello,

Above a summary of my code:

Code: Select all

 ///////////// .h files /////////////
 class prm {	
	public:
		prm(char * a, uint16_t b, bool c, bool d, int e);
		virtual ~prm();
		...
 
 class prmHandler : public prm{
	public:
		prmHandler(char * a, uint16_t b, bool c, bool d, int e, std::string f, int g, int h);
		~prmHandler();
		std::string f;
		int g, h;
		...
		
///////////// .cpp file /////////////
prmHandler::prmHandler(char * a, uint16_t b, bool c, bool d, int e, std::string f2, int g2, int h2):
		prm(a, b, c, d, e), f(f2), g(g2*1000), h(h2) {
		...
The error I have is the following:
prmHandler.cpp: In constructor 'prmHandler::prmHandler(char*, uint16_t, bool, bool, int, std::string, int, int)':
prmHandler.cpp:13:103: error: no matching function for call to 'prm::prm()'
13 | prm(a, b, c, d, e), f(f2), g(g2*1000), h(h2) {
I'm using IDF 5.1

If I create the default constructor and log both constructor, I see that only default one is called when i create prmHandler.
Where is the problem?

I obviously want to call prm(a,b,c,d,e) as i create prmHandler(a,b,c,d,e,f,g,h) , not prm().

Thanks

rsimpsonbusa
Posts: 126
Joined: Tue May 17, 2016 8:12 pm

Re: Problem with C++ and derived class

Postby rsimpsonbusa » Mon Aug 07, 2023 2:14 am

I think the problem is you declared in Class prm a Virtual destructor which means that the derived Class prmHandler MUST implement a destructor method ~prm(). Virtual means the derived class must implement that method.

brazoayeye
Posts: 41
Joined: Sun May 03, 2020 2:37 pm

Re: Problem with C++ and derived class

Postby brazoayeye » Mon Aug 28, 2023 2:57 pm

I don't get the point.

since in the code I use

Code: Select all

delete prmObj;
i have the error described here, because I must define ~prm as virtual.

Now, when i call "delete prmObj" the compiler switches between ~prm() and ~prmHandler() depending on type of prmObj.
In cpp I have implemented

Code: Select all

prm::~prm(){
}

prmHandler::~prmHandler(){
}
i tried to write failing

Code: Select all

//.h file
class prmHandler : public prm{
	public:
		...
		~prm(); // error: declaration of '~prm' as member of 'prmHandler'
		...
		
// .cpp file

prmHandler::~prm(){ // error: declaration of '~prm::prm' as member of 'prmHandler'
}
Any ideas or test i should try?

Thanks

brazoayeye
Posts: 41
Joined: Sun May 03, 2020 2:37 pm

Re: Problem with C++ and derived class

Postby brazoayeye » Tue Aug 29, 2023 7:00 am

I found the problem: I forgot a protected prm p; inside prmHandler class header hence the compiler also tried to build a prm::prm() when I build prmHandler.

Sorry for wasting your time

rsimpsonbusa
Posts: 126
Joined: Tue May 17, 2016 8:12 pm

Re: Problem with C++ and derived class

Postby rsimpsonbusa » Fri Sep 08, 2023 1:22 am

Thanks for the solution.

Who is online

Users browsing this forum: MicroController and 259 guests