2017-09-22 74 views
2

我试图用boost::assign::list_of()在类中声明一个静态集合。在使用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

有什么办法来解决这个问题? 谢谢!

+1

我想你一定会使用C++ 98? – YSC

+0

你是对的:/ – canadiadude

+0

如果下面的答案满足你,请点击答案评分正好的绿色选中标记。 – YSC

回答

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"); 
+1

你打我几秒钟:/活:http://coliru.stacked-crooked.com/a/a0d235afcaa211ba(注意:'<>'是不必要的) – YSC

+0

@YSC谢谢。将删除它们。自从我不得不使用这个优秀的图书馆已经很长时间了。 –