views:

410

answers:

6

Hi,

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?

+1  A: 

I don't think you mean overriding. And your implementation is missing a return type - it should be:

int Base::checkVal(int&  value)
anon
Apologies, I cut this out of the code. I do have the return type above..
donalmg
It would help if you posted the complete code illustrating what you are asking about.
anon
+1  A: 

You have two implementations in your Base class, one in the header file, the other in the .cpp file. Just omit the one in the header file.

Maurits Rijk
+2  A: 

You have defined the function as { return val;} in the header file, you can't also define it as

{
     if(value == 0)
        value = 10;
     return value;
}

anywhere else in your program. It can only have one definition. It may be that you meant the second definition to be for a derived class (and not Base), but that's not what you have in your code. The fact that your question doesn't use the word class at all tells me you might be confused about how virtual functions work. Read up on them some, it should be pretty obvious how to define a virtual function in a derived class.

It should look something like Derived::checkVal(int & value). note the use of "Derived" as the class name instead of "Base".

SoapBox
I have several classes which use the Base Header file, so I need the definition of this function here. Anywhere else and it would mean huge rework to update all inheriting classes.My code works as Base.h <- Another.h <- Class.cpp. For this class, I need to override the functionality of the standard Base.h function, so I made it virtual to override it in the .cpp.Is this possible?I hope this clears any confusion up..
donalmg
+2  A: 

xxx.h

#ifndef HeaderH
#define HeaderH

class Base {
  virtual int checkVal(int& val); // no definition 
};
#endif

xxx.cpp

#inlcude "xxx.h"

int Base::checkVal(int&  value)
{
    if (value == 0)
      value = 10;
    return value;
}

And it works fine. Else the compiler will not know what definition to use. There can be only one!

Kornel Kisielewicz
+5  A: 

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 .cpp file, just declare checkval() inside the base class, no need to define it there.

Your implementation of checkval() fails if you call it like the following:

Base b;
//some code
b.checkval(10);//Error because temporaries cannot be bound to non-constant references

Is there something I should include in my header file which will override the Base virtual function?

Create a derived class inside the header file and override checkval() then by declaring the function inside the derived class and defining it inside .cpp file.

Prasoon Saurav
I have several classes which use the Base Header file, so I need the definition of this function here. Anywhere else and it would mean huge rework to update all inheriting classes. My code works as Base.h <- Another.h <- Class.cpp. For this class, I need to override the functionality of the standard Base.h function, so I made it virtual to override it in the .cpp. Is this possible?
donalmg
Overriding means redefining a function(same signature) in the derived class. Create a derived class and redefine `checkval()` inside it.
Prasoon Saurav
+1  A: 

The error is exactly what you should get.

Without creating a new class that inherits from Base, you cannot redefine the function.

#ifndef DERIVED_H
#define DERIVED_H

#include "base.h"

class Derived : public Base{
<some code>  
public:
    virtual int checkVal(int& val)
    { 
       if(value == 0)
          value = 10;
       return value;
    } 
};
#endif DERIVED_H

Then use Derived where you were going to use Base. Please read Friendship and Inheritance.

Adam W