相关疑难解决方法(0)

c ++ - 为字符串赋值null

我正在学习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 …
Run Code Online (Sandbox Code Playgroud)

c++

35
推荐指数
5
解决办法
13万
查看次数

标签 统计

c++ ×1