作为参数传递的字符指针转换为字符串 我是C++新手,需要输入以下问题:
在我的头文件(MyClass.h)中,函数定义为:作为参数传递的字符指针转换为字符串 我是C++新手,需要输入以下问题:,c++,regex,visual-c++,C++,Regex,Visual C++,在我的头文件(MyClass.h)中,函数定义为: bool Function(char *InString,char *outStr); 这已在“MyClass.cpp”中实现,如下所示: bool MyClass::Function(char *InString,char *OutString=0) { std::string str = ***** I require the InString to be converted to String and assigned to str
bool Function(char *InString,char *outStr);
这已在“MyClass.cpp”中实现,如下所示:
bool MyClass::Function(char *InString,char *OutString=0) {
std::string str = ***** I require the InString to be converted to String and assigned to str.
}
在控制台的主要功能中,我使用了以下功能:
#include "MyClass.h"
int _tmain(int argc, _TCHAR* argv[]) {
char inp[50];
char output[50];
memset(output,0,sizeof(output));//Intialized
std::cin>>inp;
MyClass x;
bool m = x.Function(inp,output);
}
非常感谢您的帮助。
str.assign(InString);
您可以将
char
分配给std::string
仅std::string str=InString代码>应该可以工作 您只需将字符指针分配到字符串即可。只要字符指针指向的数据以null结尾,副本就会按预期工作
std::string str = InString;
嗯std::string
有一个构造函数:
const char *s = "\nHello, World!";
std::string str(s);
cout<<str;
char *s2 = "\nHello, World!";
std::string str2(s2);
cout<<str2;
const char*s=“\nHello,World!”;
std::字符串str(s);
我建议您开始阅读一些基本教程,因为这是一个非常简单和基本的教程。为什么您不在整个程序中普遍使用std::string
?另外,默认参数在声明中,而不是在定义中。@VittorioRomeo,除非定义也是声明。好的,我明白您的意思了。我正在详细研究空指针。我想在使用memset之后,我们初始化并使用0字符数组,它会自动放置空指针。