1
我想用具有可选值的映射初始化shared_ptr。我将在程序的后期阶段初始化这些值。如何使用可选值初始化shared_ptr映射
我读了下面的帖子,并用它作为指导:How to add valid key without specifying value to a std::map?
但是我的情况有点不同,因为我使用一个shared_ptr。事不宜迟,这是我写的代码:
ShaderProgram.h
...
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
typedef map<string, optional<GLuint> > attributes_map;
class ShaderProgram
{
public:
ShaderProgram(vector<string> attributeList);
...
private:
shared_ptr<attributes_map> attributes;
};
ShaderProgram.mm
ShaderProgram::ShaderProgram(vector<string> attributeList)
{
// Prepare a map for the attributes
for (vector<string>::size_type i = 0; i < attributeList.size(); i++)
{
string attribute = attributeList[i];
attributes[attribute];
}
}
编译器会通知我,以下错误:类型 'shared_ptr的' 不提供一个下标操作符。
任何想法可能是什么问题?
@LucDanton,同意和更新。 – hmjd 2012-07-30 10:36:38
感谢运行时错误的抬头;) – polyclick 2012-07-30 11:01:31