std::stoi missing in g++ 4.7.2? - c++

I get the error message "stoi is not a member of std" when I try to use std::stoi and try to compile it. I'm using g++ 4.7.2 from the command line so it can't be IDE error, I have all my includes in order, and g++4.7.2 defaults to using c++11. If it helps, my OS is Ubuntu 12.10. Is there something I haven't configured?
#include <iostream>
#include <string>
using namespace std;
int main(){
string theAnswer = "42";
int ans = std::stoi(theAnswer, 0, 10);
cout << "The answer to everything is " << ans << endl;
}
Will not compile. But there's nothing wrong with it.

std::stoi() is new in C++11 so you have to make sure you compile it with:
g++ -std=c++11 example.cpp
or
g++ -std=c++0x example.cpp

For older version of C++ compiler does not support stoi. for the older version you can use the following code snippet to convert a string to integer.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
string input;
cin >> input;
int s = std::atoi(input.c_str());
cout<<s<<endl;
return 0;
}

Related

Is there any way to convert string characters into integer? [duplicate]

I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}

C++ _byteswap_ulong was not declared in this scope

when I try to compile my program I get this error:
error: ‘_byteswap_ushort’ was not declared in this scope
long lNum = (long)_byteswap_ushort(iNum);
this is the program:
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
long inputNum;
cout << "Input number:\n";
cin >> inputNum;
long Num = (long)_byteswap_ulong(iNum);
stringstream oss;
oss << hex << Num;
string mystring = oss.str();
return 0;
}
I thought that including stdlib should solve the problem. Is there any other library I should include?
The program is compiled with:
g++ -m32 -o output32 prog.cpp
You seem to be trying to use a function specific to MS Visual C++ compiler, which is not available in GCC. Use an appropriate GCC builtin instead.
Seems like uint32_t __builtin_bswap32(uint32_t x) would be appropriate in this case.
Another option, if you wanted a portable solution, would be to use something like Boost Endian library.

Parsing int in C++11 - stoi

I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}

Problems with std::stoi in C++ using ubuntu

I am receiving a compile error when I try to compile the simple program below.
error: ‘stoi’ was not declared in this scope
I've tried to include both #include <string> and #include <string.h> and I still am having those issues. I am using Ubuntu and I cannot remember how I installed g++ but I am sure it was using the apt-get install g++ command, so I do not know what version of g++ or the C++ libraries I am using.
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct Data
{
string fname;
string lname;
int age;
};
int main()
{
bool toContinue = true;
Data data;
string buffer;
do
{
try
{
getline(cin,data.fname);
getline(cin,data.lname);
getline(cin,buffer);
data.age = stoi(buffer);
cout<<data.fname<<" ";
cout<<data.lname<<" ";
cout<<data.age<<endl;
}
catch(std::invalid_argument)
{
cerr<<"Unable to parse integer";
}
}while(toContinue);
return 0;
}
My goal is to be able to use exception handling in case the user enters junk for any of the variables.
If you take a look at the documentation, you'll see that it was introduced in C++11. You'll have to compile your code with the -std=c++11 option to enable those features because code isn't compiled as C++11 by default.
Drew commented saying that if you are using C++03, you can use
boost::lexical_cast<int>(buffer)
As it turns out I needed this in order for it to work...
g++ -std=c++0x ./main.cpp

Why do statements after return change the return value?

C++ returns invalid value in the following code:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
205960
When I commnet line after return, it works fine:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
//v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
1
I am using code::blocks with mingw32-g++.exe compiler. The mingw version is: gcc version 4.4.1 (TDM-2 mingw32).
Your compiler has a bug. Fortunately, it is also obsolete. You should upgrade — G++ is up to version 4.6.2, which also implements much of C++11, which is very useful.
If you choose to stick with an older compiler, that is also a decision to accept its flaws.
Edit: If you are really stuck with 4.4 (for example due to a PHB), that series is still maintained. You can upgrade to GCC 4.4.6, released just this past April.