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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:57:17+00:00 2026-06-05T15:57:17+00:00

I have a templated class Rect with conversion constructor which allows conversion between Rect

  • 0

I have a templated class Rect with conversion constructor which allows conversion between Rect to Rect and vice a versa. But when compiling the code, the compiler gives an error stating the constructor can’t access the protected members of the class.
Here is code:

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

using namespace std;

template< typename T >
class Rect{
protected:
  T width, height;
public:
  Rect(T a, T b){
    width = a;
    height = b;
  }
  template< typename U >
  Rect(Rect<U> const &r){
    width = r.width;
    height = r.height;
  }
  int area(){
    return width*height;
  }
};

int main(){
  Rect<int> a(3,4);
  Rect<float> b(a);
  cout<<b.area()<<endl;
}

And here is the compilation error:

test.cpp: In constructor ‘Rect<T>::Rect(const Rect<U>&) [with U = int, T = float]’:
test.cpp:28:18:   instantiated from here
test.cpp:10:7: error: ‘int Rect<int>::width’ is protected
test.cpp:18:5: error: within this context
test.cpp:10:14: error: ‘int Rect<int>::height’ is protected
test.cpp:19:5: error: within this context

I want to solve this problem without using template specialization and making friend classes. As far as I know you can’t declare constructors as friends. Any ideas?

Edit: I have made corrections to semantics. So the constructor I am trying to build is really a conversion constructor.

Edit2: Corrected the program.

  • 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-05T15:57:19+00:00Added an answer on June 5, 2026 at 3:57 pm

    As K-ballo mentioned, Rect<int> and Rect<float> are different types, and do not have access to each others’ private and protected members. You can explicitly allow this by adding the following template friend declaration to your class (like so):

    template <typename U> friend class Rect;
    

    Semantically, this means “for any type U, give the class Rect<U> access to my private and protected members” — it is an outgoing permission grant from every Rect instantiation to every other Rect instantiation.

    Note that this won’t be necessary if you add accessors for width and height (as K-ballo suggests) — then you can just use those accessors in the conversion constructor and forgo the friend definition entirely. I would prefer his solution over mine; I give mine merely as another possible option and to introduce to you a concept (friends, and specifically template friends) that you might not be familiar with.

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

Sidebar

Related Questions

I have non-template class with a templatized constructor. This code compiles for me. But
I have a templated class which contains a static function which does not depend
I have a templated class with a method for which I need a different
I have a templated class Child which inherits from a non-templated Parent class. When
I have a templated class (call it Foo ) which has several specializations. I
I have a templated class template <T> class Example { ... }; inside which
I have a templated base class which follows: template<class scalar_type, template<typename > class functor>
I have the following code, which is vital to understand a class I'm working
Is it possible to have a templated class and also templating the constructor with
I have a templated container class that looks something like this: template <class ItemType>

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.