schorsch312
schorsch312

Reputation: 5714

How to initialize const std::vector<MyClass>

I have a container class like this

class Container {
  public:
    Container(const std::string name, const double value)
        : name(name), value(value),{};
    const std::string name;
    const double value;
};

and I like to initialise a const std::vector<Container>.

This

const std::vector<Container> sets{{"foo", 0}, {"bar", 1}};

works fine using the intel compiler (version 15.0.3 (gcc version 4.8.2 compatibility)) and c++11 enable (-std=c++11)this works with RedHat6, but it fails under Windows7. The compiler is the very same, but the front end is visual studio 2013.

I get the error message:

 no operator "=" matches these operands
           operand types are: Container = Container
           _Right = _Move(_Tmp);

Do I need to write my own copy constructor?

The full example is

#include <vector>
#include <string>

class Container {
  public:
    Container(const std::string name, const double value) : name(name), value(value){};
    const std::string name;
    const double value;
};

int main() {
    const std::vector<Container> sets{{"foo", 0.0},{"bar", 1.0}};
}

Upvotes: 2

Views: 99

Answers (1)

Mr.C64
Mr.C64

Reputation: 42984

I tried compiling your code with VS2015, and it compiles fine.

I think you simply hit a compiler bug. I would suggest upgrading your C++ compiler to a newer version with better modern C++ support.

P.S. Note that VS2015 supports Windows 7 as well.

Upvotes: 2

Related Questions