2

I can't compile this code:

struct Base
{
    int A;
};

struct Derived : public Base
{
    int B;
};

int main()
{
    Derived d{ 1, 2 };
    return 0;
}

Error C2440 'initializing': cannot convert from 'initializer list' to 'Derived'

How can I initialize this struct as Derived d.A = 1, d.B = 2?

I'm using Visual Studio Professional 2017 Version 15.6.0.

Answer:

Upgraded to Visual Studio Professional 2019(Version 16.3.5), compiling successes with /std:c++17 option.

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
P-P
  • 1,670
  • 5
  • 18
  • 35
  • What C++ standard are you compiling for? Can you increase it to C++ 17? – user4581301 Oct 18 '19 at 02:13
  • 2
    If you are using g++ or clang, add the `-std=c++17` option, if you are using MSVC, use `/std:c++17` – ph3rin Oct 18 '19 at 02:27
  • 1
    Almost Duplicate: [How to enable C++17 compiling in Visual Studio?](https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio) – user4581301 Oct 18 '19 at 04:31
  • In Visual Studio Professional 2017, compiling failed with /std:c++17 option. In Visual Studio Professional 2019(Version 16.3.5), compiling successes with /std:c++17 option. – P-P Oct 18 '19 at 05:46
  • 1
    Aggregate initialization is out then, unfortunately. I think that leaves you with adding constructors. – user4581301 Oct 18 '19 at 16:16

0 Answers0