Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 548327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:02+00:00 2026-05-13T11:04:02+00:00

I have one header file which uses a virtual function. This is declared and

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:04:03+00:00Added an answer on May 13, 2026 at 11:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(In C++) I have a class whose structure is declared in a header file.
I have one application which uses the standard .NET forms authentication cookie, now I
I have a common HTML header file which I use in several PHP files.
I have one .cpp file that includes a few header files. These header files
I have one std::list<> container and these threads: One writer thread which adds elements
I have one aspx page with some controls. Also i have one DIV which
When you have a static global variable in a C++ header file, each translation
I have a site using a self-signed cert (which prompts for trust this site
I have one field that I need to sum lets say named items However
I have one thread that writes results into a Queue. In another thread (GUI),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.