[英]cpp: error: 'class boost::optional<std::__cxx11::basic_string<char> >' has no member named 'c_str'
I am new to c++ and trying to fix an issue in my function -我是 c++ 的新手,并试图解决我的 function 中的问题 -
Test::Test(const boost::optional<std::string>& name):
mName(name)
{
...
Some statements
...
}
int Test::setResult()
{
...
i=system(mName.c_str())
...
}
The error I am getting is我得到的错误是
error: 'class boost::optional<std::__cxx11::basic_string<char> >' has no member named 'c_str'
i=system(mName.c_str());
^
Please help to fix the my code请帮助修复我的代码
The mName
member is of the optional
type. mName
成员是optional
类型。 If you want the string behind that optional
, you need to dereference it with something like:如果您想要该optional
后面的字符串,则需要使用以下内容取消引用它:
i = system(mName->c_str());
Of course, you probably want to first ensure it has a value:当然,您可能希望首先确保它具有值:
if (mName) i = system(mName->c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.