静态c++映射初始化错误C2552:不能用初始化器列表初始化非聚合体。

时间:2021-10-08 21:01:05

I'm trying to initialize a map in a header with the following code, but it keeps giving me the error in the title. I'm using C++11, so this should be possible, right?

我尝试用下面的代码在标题中初始化一个映射,但是它一直给我标题中的错误。我用的是c++ 11,所以这应该是可能的,对吧?

typedef std::map<NPCAnimation::ID, std::map<Direction::ID, sf::Time>> AnimationSpeedMap;
AnimationSpeedMap AnimationSpeeds = {
    {NPCAnimation::WALK, {
            {Direction::LEFT, sf::milliseconds(100)},
            {Direction::RIGHT, sf::milliseconds(100)},
            {Direction::UP, sf::milliseconds(200)},
            {Direction::DOWN, sf::milliseconds(200)}
        }
    },

    {NPCAnimation::IDLE, {
            {Direction::LEFT, sf::milliseconds(600)},
            {Direction::RIGHT, sf::milliseconds(600)},
            {Direction::UP, sf::milliseconds(600)},
            {Direction::DOWN, sf::milliseconds(600)}
        }
    },

    {NPCAnimation::SPECIAL, {
            {Direction::LEFT, sf::milliseconds(500)},
            {Direction::RIGHT, sf::milliseconds(500)},
            {Direction::UP, sf::milliseconds(500)},
            {Direction::DOWN, sf::milliseconds(500)}
        }
    },
};

Thanks in advance! ~ grambler1

提前谢谢!~ grambler1

2 个解决方案

#1


14  

VS2012 supports the initializer list syntax, but the VS2012 implementation of std::map does not. You'll have to wait for support for this to be added.

VS2012支持初始化器列表语法,但是std的VS2012实现::map不支持。你需要等待支持才能加入。

#2


6  

I had the same issue. Unfortunately bracket-initialization and many other C++11 features are not supported until VS2013.

我有同样的问题。不幸的是,在VS2013之前不支持括号初始化和许多其他c++ 11特性。

proof: http://msdn.microsoft.com/en-us/library/vstudio/bb386063(v=vs.120).aspx

证明:http://msdn.microsoft.com/en-us/library/vstudio/bb386063(v = vs.120). aspx

#1


14  

VS2012 supports the initializer list syntax, but the VS2012 implementation of std::map does not. You'll have to wait for support for this to be added.

VS2012支持初始化器列表语法,但是std的VS2012实现::map不支持。你需要等待支持才能加入。

#2


6  

I had the same issue. Unfortunately bracket-initialization and many other C++11 features are not supported until VS2013.

我有同样的问题。不幸的是,在VS2013之前不支持括号初始化和许多其他c++ 11特性。

proof: http://msdn.microsoft.com/en-us/library/vstudio/bb386063(v=vs.120).aspx

证明:http://msdn.microsoft.com/en-us/library/vstudio/bb386063(v = vs.120). aspx