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

  • Home
  • SEARCH
  • 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 863891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:23:05+00:00 2026-05-15T09:23:05+00:00

I have in my header Test.h a variable of a class that doesn’t have

  • 0

I have in my header “Test.h” a variable of a class that doesn’t have a constructor without arguments.
And I have a constructor like this:

Test::Test() // <-- Here he complains:
             // error: no matching function for call to ‘Beer::Beer()’
{
   int i = 2;
   theVar = Beer(1, i); // Beer(int, int) is the only constructor
}

But I’m initializing it after the (empty) initializer list, in the constructor body.

How can I solve this? How is this problem called, if it has a name?

Thanks

  • 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-15T09:23:05+00:00Added an answer on May 15, 2026 at 9:23 am

    You need to use initializer list.

    Test::Test() : theVar(1, 2) {}
    

    If the problem is that i is the result of some other function call, you can do sometihng like

    Test::Test() : theVar(1, compute_i()) {}
    

    The problem is that the constructor body is executed after all members are initialized.

    They are initialized first by calling whichever constructors are specified in the initializer list, and if the member is not listed there, by calling its default constructor.

    That is why you get the error complaining about Beer::Beer(): because nothing else is specified, it tries to call that constructor to initialize theVar, and it doesn’t exist.

    Then after all members are initialized, the constructor body is executed (where you perform an assignment, and not an initialization, of theVar. But the compiler never gets that far, because it couldn’t perform the initialization.

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

Sidebar

Related Questions

I have a c++ header file containing a class. I want to use this
Based on the response to this question: Why does C++ have header files and
Lets say that I have a header user control in a master page, and
Why does C++ have header files and .cpp files?
I have a header <h1>My Header</h1> It has a height of 150px and a
Lets say I have a header banner on a webpage I'm about to print.
I am having trouble with IE7. I have a header, which is an IMG.
I am trying to have a table header, in a seperate div stay in
I have a couple of header files, which boil down to: tree.h: #include element.h
I have to include many header files, which are in different sub-directories. Is there

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.