Bizarre issue when populating a list with objects in C++?

后端 未结 1 1160
面向向阳花
面向向阳花 2021-01-29 13:55

I have created a class Patient and I want to populate a list of Patients with objects which I have created via the explicit constrctor. However I get a

相关标签:
1条回答
  • 2021-01-29 14:17

    The list's initializer takes a sequence of expressions, but you've given it full variable declarations instead. That's simply not valid syntax. You can only put declarations in "free space" in a function or at namespace scope, not inside another statement (we'll ignore the joys of conditionals for the purpose of this answer).

    You probably intended to create some temporaries instead:

    list<Patient> sp = {
       Patient("I.Petrov", "21.12.02", 4),
       Patient("D.Stoyanov", "12.02.97", 7),
       Patient("K.Dimitrov", "07.08.90", 1)
    };
    

    But I'd write it as:

    std::list<Patient> sp{
       {"I.Petrov",   "21.12.02", 4},
       {"D.Stoyanov", "12.02.97", 7},
       {"K.Dimitrov", "07.08.90", 1}
    };
    
    0 讨论(0)
提交回复
热议问题