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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:52:45+00:00 2026-05-18T09:52:45+00:00

I have been trying to create a templated class( Test2 ) that takes 2

  • 0

I have been trying to create a templated class(Test2) that takes 2 template arguments,Type1 and Type2. It is known that the second argument would also be a templated class that takes 2 template arguments(TypeA and TypeB).

Now, for constructing an object of Test2, I want the user to be able to use either of 2 types of constructors:

  1. One that takes objects of Type1 and Type2.
  2. One that takes objects of Type1, TypeA and TypeB.

I wrote the following code:

#include <iostream>

template<class TypeA, class TypeB>
struct Test
{
    TypeA t1obj;
    TypeB t2obj;
    Test(const TypeA& t1, const TypeB& t2)
        : t1obj(t1), t2obj(t2) {std::cout<<"Test::Type1, Type2\n";}
};


template<class Type1,
         template<typename TypeX, typename TypeY> class Type2 >
struct Test2
{
    Type1 t1obj;
    Type2<typename TypeX, typename TypeY> t2obj; //Line 17

    Test2(const Type1& t1,
          const Type2<typename TypeX, typename TypeY>& t2) //Line 20
        : t1obj(t1), t2obj(t2) { std::cout<<"Test2::Type1, Type2\n";}

    Test2(const Type1& t1,
          const TypeX& x,
          const TypeY& y)
        : t1obj(t1), t2obj(x,y) { std::cout<<"Test2::Type1, X, Y\n";}

};

int main()
{
    Test<int, char> obj1(1,'a');

    Test2<int, Test<int, char> > strangeobj1(10,obj1);
    Test2<int, Test<int, char> > strangeobj2(1,2,'b');

}

I have tried a lot but I get really absurd errors like:

wrong number of template arguments (1, should be 2) on Line 17 and 20.

  • 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-18T09:52:45+00:00Added an answer on May 18, 2026 at 9:52 am

    It doesn’t work like that. Test<int, char> is a full blown type, instead of a template. So you need type parameters

    template<class Type1,
             class Type2 >
    struct Test2
    {
        Type1 t1obj;
        Type2 t2obj; //Line 17
    
        Test2(const Type1& t1,
              const Type2& t2) //Line 20
            : t1obj(t1), t2obj(t2) { std::cout<<"Test2::Type1, Type2\n";}
    
        Test2(const Type1& t1,
              const typename Type2::a_type& x,
              const typename Type2::b_type& y)
            : t1obj(t1), t2obj(x,y) { std::cout<<"Test2::Type1, X, Y\n";}
    
    };
    

    For getting TypeX and TypeY it’s useful to export them so you can use them in Test2 as shown above.

    template<class TypeA, class TypeB>
    struct Test
    {
        typedef TypeA a_type;
        typedef TypeB b_type;
    
        // and using them, to show their meaning
        a_type t1obj;
        b_type t2obj;
    
        Test(const a_type& t1, const b_type& t2)
            : t1obj(t1), t2obj(t2) {std::cout<<"Test::Type1, Type2\n";}
    };
    

    Be sure to read Where to put the “template” and “typename” on dependent names to understand why and when to use typename before type names like above.

    • 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.