1

所以,基本上这就是我的代码中出现问题的地方。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>

void main()
{
    double k = 10.0;
    string out;
    out = "V";
    out += ".";
    out << k;   <---
}

我尝试编译,但出现此错误:

错误 C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : 无法推导出 'std::basic_ostream<_Elem,_Traits 的模板参数> &&' 来自 'std::string'

...即指向带箭头的线。我究竟做错了什么?

4

3 回答 3

2

使用std::stringstreamboost::lexical_cast

out += boost::lexical_cast<std::string>(k);

或者std::to_string如果你可以使用 C++11

于 2012-07-22T02:22:52.597 回答
1

尝试以下操作:-

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
于 2012-07-22T02:20:52.467 回答
1

您正在尝试使用 astring而不是stringstream. 没有<<定义将string作为其第一个参数的运算符,这是编译器试图告诉您的(以一种相当神秘的方式)。

stringstream out;
out << "V." << k;
string s = out.str();

如果你使用 C++11,你可以这样写:

double k = 10.0;
string out;
out = "V";
out += ".";
out += to_string(k);
于 2012-07-22T02:21:05.673 回答