[英]C++ no suitable constructor exists to convert from to
I'm trying to make it so an input of a string gets multiplied by two the subtract 1. I know that my question has been asked here before, but the problem is that I couldn't really comprehend what it was saying was wrong.我正在尝试使字符串的输入乘以减去 1 的两倍。我知道我的问题以前在这里被问过,但问题是我无法真正理解它所说的错误。
string coffeeCode(string input) { //Coffee code= 2n-1 where n=a number in a string
vector<double> userInputDoubles;
istringstream converter(input);
string token;
double value{};
while (getline (converter, token, ',')) {
value = stod(token);
value = 2 * value - 1;
userInputDoubles.push_back(value);
};
return value;
};
The function returns a double but its return type is string. function 返回一个双精度,但它的返回类型是字符串。 There is no default constructor to convert double to string.没有将双精度转换为字符串的默认构造函数。 Return a string:返回一个字符串:
std::string coffeeCode(std::string input) { //Coffee code= 2n-1 where n=a number in a string
std::vector<double> userInputDoubles;
std::istringstream converter(input);
std::string token;
double value{};
while (std::getline (converter, token, ',')) {
value = std::stod(token);
value = 2 * value - 1;
userInputDoubles.push_back(value);
};
return std::to_string(value);
};
or return a double:或返回双精度:
double coffeeCode(std::string input) { //Coffee code= 2n-1 where n=a number in a string
std::vector<double> userInputDoubles;
std::istringstream converter(input);
std::string token;
double value{};
while (std::getline (converter, token, ',')) {
value = std::stod(token);
value = 2 * value - 1;
userInputDoubles.push_back(value);
};
return value;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.