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 8014779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:02:51+00:00 2026-06-04T20:02:51+00:00

class A { public: A(int v) { _val = v; } private: int _val;

  • 0
class A {
public:
    A(int v) {
        _val = v;
    }

private:
    int _val;
};

class B {
public:
    B(int v) {
        a = A(v); // i think this is the key point
    }

private:
    A a;
};

int main() {
    B b(10);

    return 0;
}

compiler says:

test.cpp: In constructor ‘B::B(int)’:
test.cpp:15: error: no matching function for call to ‘A::A()’
test.cpp:5: note: candidates are: A::A(int)
test.cpp:3: note:                 A::A(const A&)

I’ve learned Java, and I don’t know how to deal with this in C++.
Searching for a couple days, plz tell me can C++ do this?

  • 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-06-04T20:02:52+00:00Added an answer on June 4, 2026 at 8:02 pm

    You need to use a Member Initialization List

    B(int v):a(v)
    {
    }
    

    With:

    B(int v) 
    {
            a = A(v); // i think this is the key point
    }
    

    a is being assigned a value and not being initialized(which is what you intend), Once the body of an constructor begins {, all its members are already constructed.

    Why do you get an error?
    The compiler constructs a before constructor body { begins, the compiler uses the no argument constructor of A because you didn’t tell it otherwiseNote 1, Since the default no argument constructor is not available hence the error.

    Why is the default no argument constructor not generated implicitly?
    Once you provide any constructor for your class, the implicitly generated no argument constructor is not generated anymore. You provided overloads for constructor of A and hence there is no implicit generation of the no argument constructor.

    Note 1
    Using Member Initializer List is the way to tell the compiler to use particular overloaded version of the constructor and not the default no argument constructor.

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

Sidebar

Related Questions

public class Ex7 { private int fld; private void meth(int val) { fld =
Say I have this class: public class Account { public int AccountID { get;
I have this (simplified) class: public class StarBuildParams { public int BaseNo { get;
class Memory{ private int[] memoryArray; private int size; public Memory(int n) {size = n;
I have a class: public class CarList { public int quantity{get;set;} public List<Car> Cars
I have a class: public class MyClass { public int MyMethod() { Random rand
I have made a class : public class Node { public int Id {
Consider the following class public class Class1 { public int A { get; set;
I have the following class: public class Account { public int AccountID { get;
I have the following class: public class Day { public int Date { get;

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.