如何修复对strstr没有匹配函数调用的问题
How to fix no matching function call to strstr
所以我一直收到这个错误,对'strstr(str::string&, const char [2])'
的调用没有匹配函数
我已经看到了一些通过添加.str().c_str();
来修复它的方法
但这对我来说似乎很棘手。对于我给定的场景,有没有一种更标准的方法:
string resistance = stockIterator->getValue()->getOther();
if(strstr(resistance, "R")){
printf("Small resistance");
float first = atoi(strtok(resistance, "R"));
float second = atoi(strtok(NULL, " n"));
second = second/10;
float num = (first+second)/1000000;
num = num * stockIterator->getValue()->getStockCount();
printf(" %.7fnn", num);
count = count + num;
您确实应该使用std::string
的find()
成员函数。要获得子字符串,可以使用std::string
的substr()
成员函数,而对于浮点转换,可以使用std::stof()
。以下是您的操作方法:
string resistance = stockIterator->getValue()->getOther();
std::string::size_type pos = resistance.find("R");
if(pos != std::string::npos) {
float first = std::stof(resistance.substr(0, pos));
float second = std::stof(resistance.substr(pos+1));
// other code
}
这不是"hacky"—std::string::c_str()
为您提供C++字符串的C字符串表示,如果您要混合C++字符串和C库函数(如strstr
),这是您仅有的选项。
我给你最好的建议是停止在C++中使用C库函数。您应该使用std::string::find
来查找std::string
中的字符'R'
。
使用std::string::find
strstr
适用于C字符串
相关文章:
- 将值推送到 getter 函数调用的列表中时出现问题
- 在C++中实现类似 python "map"函数的问题:调用类成员函数
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 关于C++从派生类调用在基类中实现的虚拟函数的问题
- 从 Qt 中的信号调用成员函数的问题
- 我正在尝试解决调用没有匹配函数的问题
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 头文件会导致函数调用后原始数组出现问题
- OVerload函数调用问题
- 尝试构造包装器测量函数调用时间时出现问题
- 试图在具有std ::任何构造函数的基类中调用复制构造函数的问题
- C++ 嵌套构造函数调用问题
- 构造函数调用问题
- 关于从C++调用C#函数的问题,在windows phone上反向P/Invoke
- 将C++中涉及数组的函数调用转换为C#时出现问题
- 如何修复对strstr没有匹配函数调用的问题
- 通过初始化列表调用另一个类的构造函数.有问题
- 推导函数调用时出现歧义问题
- 调用接受三个参数的函数出现问题,其中一个是 int const
- 函数调用出现问题