I have one header file which uses a virtual function.
This is declared and defined:
#ifndef HeaderH
#define HeaderH
class Base {
<some code>
public:virtual int checkVal(int& val) { return val;}
};
#endif
I have another header file which declares some functions, and inherits from this base header.
Finally, I have the implementation of this header file in another .cpp file:
I want to override the virtual function checkVal in my implementation here, but I keep getting a redefinition error.
int Base::checkVal(int& value)
{
if(value == 0)
value = 10;
return value;
}
Is there something I should include in my header file which will override the Base virtual function?
You have already provided the definition of
checkval()inside your base class, so why are you redefining it? You can define it only once.If you want to give the implementation inside the.cppfile, just declarecheckval()inside the base class, no need to define it there.Your implementation of
checkval()fails if you call it like the following:Create a derived class inside the header file and override
checkval()then by declaring the function inside the derived class and defining it inside.cppfile.