C ++错误将字符串转换为双精度 [英] C++ error converting a string to a double
问题描述
我试图将字符串转换为双精度。代码很简单。
I am trying to convert a string to a double. The code is very simple.
double first, second;
first=atof(str_quan.c_str());
second=atof(extra[i+1].c_str());
cout<<first<<" "<<second<<endl;
quantity=first/second;
当试图转换额外的,编译器抛出这个宝石的智慧在我:
when trying to convert extra, the compiler throws this gem of wisdom at me:
错误:请求成员c_str in
extra.std :: basic_string< _CharT,
_Traits,_Alloc> :: operator [与_CharT = char,_Traits = std :: char_traits,_Alloc =
std :: allocator(i + 1))),这是非类
类型char
error: request for member c_str in extra.std::basic_string<_CharT, _Traits, _Alloc>::operator[] with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator(i + 1))), which is of non-class type char
我不知道这是什么意思。如果我cout extra [i + 1],我得到3.如果我把额外的作为一个字符串,程序试图首先(2)51(ascii为3)。
I have no idea what that means. if I cout extra[i+1], I get 3. If I leave extra as a string, the program tries to div first (2) by 51 (ascii for 3). What the heck is going on?
推荐答案
这听起来像 extra
a std :: string
,因此 extra [i + 1]
返回 code>,这是非类类型。
It sounds like extra
is a std::string
, so extra[i+1]
returns a char
, which is of non-class type.
听起来你正在尝试解析字符串 extra
从 i + 1
开始。您可以使用:
It sounds like you are trying to parse the string extra
starting from the i+1
th position. You can do this using:
second = atof(extra.substr(i + 1).c_str());
这篇关于C ++错误将字符串转换为双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!