简体   繁体   English

cpp:错误:'类提升::可选<std::__cxx11::basic_string<char> &gt;' 没有名为 'c_str' 的成员</std::__cxx11::basic_string<char>

[英]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.

相关问题 的std ::矢量 <std::basic_string<char> &gt; :: const_iterator&#39;没有名为&#39;c_str&#39;的成员 - std::vector<std::basic_string<char> >::const_iterator’ has no member named ‘c_str’ 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 错误:没有匹配的函数调用&#39;std :: map <std::__cxx11::basic_string<char> - error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char> 错误:“operator+=”不匹配(操作数类型为“long double”和“std::__cxx11::basic_string”<char> &#39;) - error: no match for 'operator+=' (operand types are 'long double' and 'std::__cxx11::basic_string<char>') 错误:无法转换 'std::__cxx11::basic_string<char> ::迭代器'</char> - error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’ 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::basic_string(int&amp;)&#39; - error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’ OSX“错误:无法转换&#39;const std :: __ cxx11 :: basic_string”是什么意思 <char> “ 意思? - OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean? C++ 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::附加<int> (int, int)&#39; - c++ error: no matching function for call to ‘std::__cxx11::basic_string<char>::append<int>(int, int)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM