【问题标题】:cannot be initialized by a non-constant expression when being declared when using boost::assign::list_of使用 boost::assign::list_of 时声明时不能由非常量表达式初始化
【发布时间】:2017-09-22 13:57:58
【问题描述】:

我正在尝试使用boost::assign::list_of() 在类中声明一个静态集。

MyClass.h

class MyClass
{
    public:
        static std::set<std::string> & formats_set();
    private:
        static const std::set<std::string> formats_;
}

MyClass.cpp

const std::set<std::string> MyClass::formats_ = boost::assign::list_of(
    "Format1"
   ,"Format2"
   ,"Format3");

但是 - 当我尝试编译时出现错误 ‘MyClass::formats_’ cannot be initialized by a non-constant expression when being declared

有没有办法解决这个问题? 谢谢!

【问题讨论】:

  • 我猜你一定会用c++98?
  • 你是对的:/
  • 如果下面的答案让您满意,请点击答案分数右侧的绿色复选标记。

标签: c++ boost c++98


【解决方案1】:

现在让我们用正确的语法来试试吧:

#include <string>
#include <set>
#include <boost/assign/list_of.hpp> // for 'list_of()'

class MyClass
{
    public:
        static std::set<std::string> & formats_set();
    private:
        static const std::set<std::string> formats_;
};

const std::set<std::string> MyClass::formats_ = boost::assign::list_of
   ("Format1")
   ("Format2")
   ("Format3");

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
相关资源
最近更新 更多