我正在学习C++.我有以下代码,但它给出了错误.
#include <iostream>
#include <string>
using namespace std;
int setvalue(const char * value)
{
string mValue;
if(value!=0)
{
mValue=value;
}
else
{
mValue=0;
}
}
int main ()
{
const char* value = 0;
setvalue(value);
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以想要创建一个接受char指针的函数,我想传递一个指向它的指针.该函数将指针指定给其成员变量.我故意传递一个空指针.以下是我得到的错误:
D:\CPP\TestCP.cpp In function `int setvalue(const char*)':
note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
Run Code Online (Sandbox Code Playgroud)
它主要是抱怨线:mValue = 0;
为什么抱怨这条线?我不能给String赋一个null?
Jam*_*lis 65
我不能给String赋一个null?
号 std::string不是指针类型; 它不能成为"null".它不能表示缺少值,这是一个空指针用于表示的值.
它可以由空,通过分配一个空字符串到它(s = ""或s = std::string())或通过清除它(s.clear()).
tem*_*def 17
您不能分配NULL或0C++ std::string对象,因为该对象不是指针.这是与C风格字符串的一个关键区别; C风格的字符串可以是NULL有效的字符串,而C++ std::string总是存储一些值.
没有简单的解决方法.如果您想保留一个标记值(比如空字符串),那么您可以执行类似的操作
const std::string NOT_A_STRING = "";
mValue = NOT_A_STRING;
Run Code Online (Sandbox Code Playgroud)
或者,您可以存储指向字符串的指针,以便您可以将其设置为null:
std::string* mValue = NULL;
if (value) {
mValue = new std::string(value);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
有两种方法可以考虑,它们可以在处理 C 风格字符串的空指针时达到相同的效果。
三元运算符
void setvalue(const char *value)
{
std::string mValue = value ? value : "";
}
Run Code Online (Sandbox Code Playgroud)
或者简单的 if 语句
void setvalue(const char *value)
{
std::string mValue;
if(value) mValue = value;
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,value仅mValue当不是空指针时才value分配给它。在所有其他情况下(即当value 为null 时),mValue将包含一个空字符串。
三元运算符方法可能有助于在缺少来自 的值的情况下提供替代的默认字符串文字value:
std::string mValue = value ? value : "(NULL)";
Run Code Online (Sandbox Code Playgroud)
我不会争辩说这是一个好主意(或nullptr与不是指针的事物一起使用的语义),但创建一个提供“可为空”语义的类相对简单(请参阅nullable_string)。
但是,这更适合 C++17 的std::optional:
#include <string>
#include <iostream>
#include <optional>
// optional can be used as the return type of a factory that may fail
std::optional<std::string> create(bool b)
{
if (b)
return "Godzilla";
else
return {};
}
int main()
{
std::cout << "create(false) returned "
<< create(false).value_or("empty") << std::endl;
// optional-returning factory functions are usable as conditions of while and if
if (auto str = create(true))
{
std::cout << "create(true) returned " << *str << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
std::optional,如示例中所示,可以转换为bool,或者您可以使用该has_value()方法,有访问错误的异常等。这为您提供了可为空的语义,这似乎是Maria试图完成的。
如果您不想等待 C++17 兼容性,请参阅有关 Boost.Optional 的答案。
| 归档时间: |
|
| 查看次数: |
127579 次 |
| 最近记录: |