0

I'm trying to convert a character from a c string to an int but I keep running into an error.

Here's my code

while(std::getline(file, line)){ if(std::isdigit(line[0]) && std::isspace(line[1]) && std::isdigit(line[2])){ SequenceArray.push_back(line); if(std::stoi(line[2])== (SequenceArray.size() -1)){ std::cout<< "Success" << std::endl; 

The error that I keep getting is as follows:

 a1.cpp: In function ‘int main(int, char**)’: a1.cpp:30:25: error: call of overloaded ‘stoi(char&)’ is ambiguous if(std::stoi(line[2])== (SequenceArray.size() -1)){ ^ a1.cpp:30:25: note: candidates are: In file included from /usr/include/c++/4.8/string:52:0, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/ostream:38, from /usr/include/c++/4.8/iostream:39, from a1.cpp:1: /usr/include/c++/4.8/bits/basic_string.h:2823:3: note: int std::stoi(const string&, std::size_t*, int) <near match> stoi(const string& __str, size_t* __idx = 0, int __base = 10) ^ /usr/include/c++/4.8/bits/basic_string.h:2823:3: note: no known conversion for argument 1 from ‘char’ to ‘const string& {aka const std::basic_string<char>&}’ /usr/include/c++/4.8/bits/basic_string.h:2926:3: note: int std::stoi(const wstring&, std::size_t*, int) <near match> stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) ^ /usr/include/c++/4.8/bits/basic_string.h:2926:3: note: no known conversion for argument 1 from ‘char’ to ‘const wstring& {aka const std::basic_string<wchar_t>&}’ a1.cpp:35:6: warning: label ‘std’ defined but not used [-Wunused-label] std:exit(EXIT_FAILURE); 
2
  • I can't be the only one to think that the compiler's translation of the code's line[2] into the message's line.std::basic_string<_CharT, _Traits, _Alloc>::operator[]<char, std::char_traits<char>, std::allocator<char> >(2ul) is insane. Commented Feb 5, 2015 at 13:18
  • @molbdnilo g++ 4.9 made big improvements in its error messages over g++ 4.8 Commented Feb 5, 2015 at 13:20

4 Answers 4

3

A char implicit converts to a int, you don't need to use extra functions.

'a' = 97, 'b' = 98, 'c'=99, etc., following the ASCII table

So if you write,

char a_char = 'a'; int a_val = a_char; cout << a_val << endl; 

you have:

97 
Sign up to request clarification or add additional context in comments.

Comments

1

For std::stoi missing, try #include <string> (and enable C++11). However see also this thread - the Windows ports of g++ have had a long-standing issue with support of stoi and to_string.

The second error is that std:exit should be std::exit.

The third error is because of line[2].c_str(). You have not told us what line is but the error message suggests it is a std::string. So line[2] is a char and char does not have any member functions. If you explain what you are trying to do in the code std::atoi(line[2].c_str()) someone will be able to help. Maybe you meant line[2] - '0' which will give an integer between 0 and 9 if the third character in the line was a digit.

3 Comments

@Clockwork again line[2] is a char so you cannot call stoi on it. char and string are different. A char is not just a short string. If line[2] - '0' is not what you want then please explain in words what you want that piece of code to do , as it is not clear.
How do I convert a char to an int in that case. It is the number being retrieved by getline( ) as a char
0

std::stoi() is C++11. Not all compilers enable C++11 by default.

Comments

0

The first error is because you haven't enabled C++11 support. GCC currently chooses C++03 by default, and stoi didn't exist in that version.

Add -std=c++11 to the compiler's arguments. If that doesn't work, try -std=c++0x, and think about getting a more up-to-date compiler. If you're stuck with an ancient compiler, then use atoi as in the code you originally posted (or perhaps something involving strtol, if you want to detect errors).

Also make sure you've included <string> for the declaration of that function.

The second error is because you wrote : instead of ::.

1 Comment

@Clockwork: As I said, add -std=c++11 to the compiler arguments. In your case, add it to the definition of CXXFLAGS in the makefile.