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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:59:42+00:00 2026-06-05T10:59:42+00:00

From what I understand about inheritance in C++ is that whenever the constructor of

  • 0

From what I understand about inheritance in C++ is that whenever the constructor of a child class is called, constructor of the parent class is called automatically. And as for templated constructors, data type of the template argument is infered automatically i.e. we don’t need to specify template arguments separately. The program generates a compilation error which I don’t seem to understand.

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

class A{
  public:
    int x;
    int y;
    int first(){
      return x;
    }
    int second(){
      return y;
    }
};

class C{
  public:
    float a,b;
    C(){
      a = 0.0f;
      b = 0.0f;
    }
    template<class T>
      C(T t){
        a = t.first();
        b = t.second();
      }
};

class D: public C{
  public:
    float area(){
      return a*b; 
    }
}

int main(){
  A a;
  a.x = 6;
  a.y = 8;
  C c(a);
  D d(a);
  cout<<c.a<<" "<<c.b<<" "<<d.area()<<endl;
}

Compilation error generated

test.cpp: In function ‘int main()’:
test.cpp:56:8: error: no matching function for call to ‘D::D(A&)’
test.cpp:56:8: note: candidates are:
test.cpp:44:7: note: D::D()
test.cpp:44:7: note:   candidate expects 0 arguments, 1 provided
test.cpp:44:7: note: D::D(const D&)
test.cpp:44:7: note:   no known conversion for argument 1 from ‘A’ to ‘const D&’

I have no idea what is happening here. Any ideas?

  • 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-05T10:59:43+00:00Added an answer on June 5, 2026 at 10:59 am

    D has to pass the constructor argument to C, since you are not using default constructors.

    class D : public C {
    public:
        template <typename T> D (T t) : C(t) {}
        float area () { /* ... */ }
    };
    

    The reason for the error is that you are trying to construct D with a parameter, but have not declared any constructor that would allow you to do so. Furthermore, you have to pass the parameter into C, otherwise the compiler will use C‘s default constructor.

    The compiler error message can be analyzed like this.

    test.cpp:56:8: error: no matching function for call to ‘D::D(A&)’
    

    The compiler is complaining about:

    D d(a);
    

    And that it can’t figure out how to construct a D when passed something of type A.

    It then presents the two choices of constructors it knows about:

    test.cpp:44:7: note: D::D()
    test.cpp:44:7: note: D::D(const D&)
    

    And it points out that for each one, there is a reason it can’t use it. For the first one, it doesn’t take any arguments. For the second one, it has no way to convert something of type A into type D.

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

Sidebar

Related Questions

From what I understand about the HTTP protocol is that it is stateless. This
From what I understand about Windsor Container and MVC applications, is that there should
I've been reading up on assembly. From what I understand about programming languages, it
I have read about session fixation and from what I understand it forces a
I've read about fork and from what I understand, the process is cloned but
I found that Maven implies specific directory layout. But I don't understand from here:
From what I understand PRNG uses a seed that generates a sequence of numbers
From what I understand, it should automatically have it installed. What I have installed
Trying to understand a little more about Crockford's approach to Prototypical Inheritance whereby he
I was curious about C++ and virtual inheritance - in particular, the way that

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.