0

Given the below (omitted functions/data members) - how do I initialize the variable m_Class1Obj in Class3 using the templates constructor?

template<class T> class ClassTemplate { protected: unsigned int m_Val; public: ClassTemplate(unsigned int val = 128) { m_Val=val; } }; class Class1 : public ClassTemplate<BYTE> { }; class Class3 { protected: Class1 m_Class1Obj; public : Class3() : m_Class1Obj(64) { }; // doesn't work - error C2664: 'Class1::Class1(const Class1 &)': cannot convert argument 1 from 'int' to 'const Class1 &' }; 

TIA!!

11
  • Can't replicated with the shown code after fixing the missing semicolon. godbolt.org/z/8G9as9cj4 Commented Apr 5, 2024 at 21:15
  • 2
    It's a C++17 / C++ 20 difference see godbolt.org/z/dWrzKEjvb . Change the standard to C++20 and they all compile. Commented Apr 5, 2024 at 21:18
  • 2
    Aren't you missing using ClassTemplate::ClassTemplate; inside Class1? In C++20 this works thanks to this Commented Apr 5, 2024 at 21:24
  • using ClassTemplate<BYTE>::ClassTemplate; inside of Class1 augments issue. Formally Class1 didn't have constructor that takes an int Commented Apr 5, 2024 at 21:27
  • @Swift-FridayPie maybe I'm misinterpreting something, but to me this was never supposed to work pre C++20. Class1 being a member, inherited from template etc. does not change anything here. Commented Apr 5, 2024 at 21:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.