Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to_string not declared in scope

I am trying to make the to_string(NUMBER) function work in my Ubuntu computer for weeks but it never ever works in the QT environment or anywhere else. My code works perfectly on my Mac osx, but when I try running it in Ubuntu it complains that to_string is not declared in scope. Any solutions to this would be greatly appreciated. I have tried to update the gcc compiler but it didn't fix the problem. Please help.

I am using QT Creator 4.8.1 and I am using C++ and latest version of Ubuntu.

int Bint::operator*(int temp){
    Bint b(to_string(temp));
    return ((*this)*b);
}

I was missing the QMAKE_CXXFLAGS += -std=c++0x in the pro file.

like image 811
Muhammad Sibghat Khan Oreo Avatar asked Mar 22 '13 11:03

Muhammad Sibghat Khan Oreo


4 Answers

There could be different reasons why it doesn't work for you: perhaps you need to qualify the name with std::, or perhaps you do not have C++11 support.

This works, provided you have C++11 support:

#include <string>

int main()
{
  std::string s = std::to_string(42);
}

To enable C++11 support with g++ or clang, you need to pass the option -std=c++0x. You can also use -std=c++11 on the newer versions of those compilers.

like image 124
juanchopanza Avatar answered Oct 21 '22 01:10

juanchopanza


you must compile the file with c++11 support

g++ -std=c++0x  -o test example.cpp
like image 23
The Beast Avatar answered Oct 20 '22 23:10

The Beast


I fixed this problem by changing the first line in Application.mk from

APP_STL := gnustl_static

to

APP_STL := c++_static
like image 3
MikeC Avatar answered Oct 20 '22 23:10

MikeC


You need to make some changes in the compiler. In Dev C++ Compiler: 1. Go to compiler settings/compiler Options. 2. Click on General Tab 3. Check the checkbox (Add the following commands when calling the compiler. 4. write -std=c++11 5. click Ok

like image 1
ashwani nagar Avatar answered Oct 20 '22 23:10

ashwani nagar