打印字符串的所有子字符串时出错
Error in printing all substrings of a string
这是参照Synxis的以下回答。
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715 # 18715
假设,我必须打印字符串"cbaa"的所有子字符串。要做到这一点,我必须像这样调用方法:
findAllSubstrings2("cbaa");
如果我从user获取一个字符串,并执行以下操作:
string s;
cin>>s;
findAllSubstrings2(s);
给出如下错误:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
为什么会发生这种情况?
当您试图传递类型为std::string
的参数时,错误信息显示函数findAllSubstrings2
的参数声明为类型为const char *
string s;
//...
findAllSubstrings2(s);
应该使用std::string
类的成员函数c_str
或data
(从c++ 11开始)。例如
findAllSubstrings2(s.c_str());
您使用的是字符串,在函数是char尝试使用char[] s;
在传递参数
时在string类中使用c_str()方法string s;
cin>>s;
findAllSubstrings2(s.c_str());
您可能应该更改函数的参数类型。Somethink:
void findAllSubstrings2(string s){
//... function implementation...
}
相关文章:
- 重载操作程序时出错>>用于类中的字符串 memebr
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 在 Rcpp 中的字符串类型之间转换时出错
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 读取字符串C++的字符时出错
- 读取字符串字符时出错 - 对于数组中的字符串值
- 通过字符串::插入插入字符时出错
- 使用来自串行端口的字符串数据来操作振镜扫描仪在更高的速度下会出错
- 通过 constexpr 中的 'const char *' glvalue 访问字符串文字的值会出错
- 读取字符串字符时出错
- 计算字符串中字符的出现次数时出错
- 在字符串中反转元音时出错
- C++ 使用 Strtok 读取字符串字符时出错
- 在 c++ 中调用将字符串作为参数传递的函数时出错
- 使用 C++Builder 项目读取字符串中的文件时出错
- 在字符串流中使用来自 github 的代码时出错
- C ++ ifstream 无法读取字符串,并且在读取字符时出错
- 需要帮助以了解字符串出错的地方
- cin和字符串出错