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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:39:33+00:00 2026-05-23T12:39:33+00:00

Couldn’t find anything relevant in forums So ,Please help me with this code .I’m

  • 0

Couldn’t find anything relevant in forums So ,Please help me with this code .I’m brushing up my c++ concepts and met with a strange error

#include<iostream>
using namespace std ;
class base
{
    int i ;
public:
    virtual void f(){cout<<"base" ; return ;};
};
class derived: public base
{
int j ;
public:
    void f() {cout<<"derived" ; return ;}
};
template<class T>
class test
{
public:
    test(T b) 
    {
        b.f(); cout<<endl<<" "<<sizeof(b)<<endl;
    }
};
int main()
{
base b ;
derived d;
test<derived> t(b); // cannot instantiate user defined type without typename
}

The following code fails to compile with the following error :

test.cpp: In function ‘int main()’:
test.cpp:28: error: no matching function for call to ‘test<derived>::test(base&)’
test.cpp:19: note: candidates are: test<T>::test(T) [with T = derived]
test.cpp:17: note:  test<derived>::test(const test<derived>&)

I can make wild guess and arrive at an answer at to why did this happen .If i instantiate a new base class from the template , everything works just fine , but not this one .
Can somebody tell me a good source for template instantiations and what are the rules/semantics , what is happening behind the curtain ?
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-23T12:39:33+00:00Added an answer on May 23, 2026 at 12:39 pm

    base is not a complete derived type, so you’ll have to provide a constructor inside your template that fills in the missing details.

    template<class T> //original template
    class test
    {
      public:
        test(T b)
        {
          b.f();
          cout<<endl<<" "<<sizeof(b)<<endl;
        }
    };
    

    When you create an instance of this template based on derived, the compiler translates it to a class, which essentially boils down to this

    class derivedtest
    {
      public:
        derivedtest(derived b)
        {
          b.f();
          cout<<endl<<" "<<sizeof(b)<<endl;
        }
    };
    

    The default constructor is not generated any more. However, a default copy constructor is still created.

    derivedtest::derivedtest(derived const&);
    

    As you can see, there is no way to pass base (by reference or copy) into the class.

    The solution is to provide a constructor inside your template that fills in the missing details:

    template<class T>
    class test
    {
      public:
        test(base const& item)
         : base(item)
        {
        }
    
    
        test(T b)
        {
          b.f();
          cout<<endl<<" "<<sizeof(b)<<endl;
        }
    };
    

    By the way, your base should most likely have a virtual destructor

    and test(T b) should be test(T const& b)

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

Sidebar

Related Questions

No related questions found

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.