我正在自学 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;
}
所以想创建一个接受 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>]
它基本上是在抱怨 line: mValue=0;
为什么抱怨这条线?我不能为字符串分配空值?