Error with stoi and debugged with gdb [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My OS is ubuntu 14.04, laptop, i7.
The g++ version is g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2.
I tried to run a simple code to test stoi:
#include <string>
int main()
{
std::string s = "123";
int i = std::stoi(s);
}
When I compile it with: g++ -g prueba2.cpp, I get:
prueba2.cpp: In function ‘int main()’:
prueba2.cpp:6:12: error: ‘stoi’ is not a member of ‘std’
int i = std::stoi(s);
^
When I debug it twice first with g++ -std=c++0x -g prueba2.cpp (I also tried with -std=c++11) and then with dbg, I got:
Then, I also did a simple search and followed the suggestions made in here1, here2 and here3, and none worked.
Am I doing something silly?

Yeah, I think you're doing something pretty silly. You probably compiled the first code, which doesn't have the std::cout statement, and you probably executed the compilation steps without -std=c++11 which would result in std::stoi not being included beecause std::stoi is from C++11 and onward. The result is still the old executable which prints out nothing.
Recompile using -std=c++11 and make sure that you saved your file correctly. Your code clearly works.
Note: the vanilla port of GCC of MinGW on Windows is flawed and has a few bugs related to C++11 and onwards; using MinGW-w64, if you ever decide to compile on Windows, can help the problem.

std::stoi is a C++11 feature. Therefore your code only compiles, if you use the -std=c++11 flags (or the equivalent -std=c++0x flag that you mentioned, which has nothing to do with debugging).
The terminal session you provided also shows that compilation works with those flags and your program runs fine without any problem. If you want to print the parsed result, you can do it like that: std::cout << i << std::endl
If you don't want to use C++11 features, you can use the >> stream operator to parse your string to an int:
stringstream ss(s);
int i;
ss >> n;
But beware: Other than with stoi, you won't get an exception, if your input doesn't contain a valid number. You will have to check the stream's status yourself.

Related

C++ : Procedure entry point couldn't be located [duplicate]

I am having immense difficulty trying to use std::string. The program compiles absolutely fine, but when I run the program, I receive this error: error
I have looked everywhere on google and haven't found a thing on how to solve this issue. I have also tried writing my code differently to see if that would tell me anything.
std::string yourName;
std::cout << "What is your name?";
std::cin >> yourName;
std::cout << "Hello, " << yourName << std::endl;
Despite the rewrite, I am still getting the same exact error, and I am not forgetting to include the iostream and string header files.
I am using the GCC compiler and my compile code is g++ -std=c++14 test.cpp -o test.exe. I have also tried using the compile code g++ test.cpp -o test.exe. This did not fix the error.
I have asked this question before on Stack Overflow, but I failed to get a clear and understandable answer, most likely due to the fact that I am a complete beginner in programming. That being said, can someone please give me an answer on what might be causing this problem and how to fix it in plain English?
You can use
g++ <file> -static-libstdc++ -static-libgcc
I had the same problem, but it worked for me
I downloaded GCC from this link: Source Forge and reinstalled GCC. My problems were successfully fixed.

Same STL files with different compilers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There are two binary files obtained from the same source file: one compiled with clang++-3.6 and the other one with g++-4.8. In a call to a function from the stl (std::unique, in particular) gdb brings me to the same file: /usr/include/c++/4.8/bits/stl_algo.h.
I expected that the implementations would be different for each compiler though. Do clang and gcc share parts of their C++ implementations?
I expected that the implementations would be different for each compiler though. Do clang and gcc share parts of their C++ implementations?
It's not that they share the same C++ implementations, it is rather that both compilers link with the same standard c++ library by default on your system.
I presume you are on linux, almost all programs installed from package manager link against libstdc++ (provided by g++).
By default, even when compiling with clang++, libstdc++ is used, so when you include iostream for example, it uses the one from /usr/include/c++/4.8.
If you want to link against llvm c++ library, you need to install the "libc++-dev" package (name may vary depending on your distro) and compile using: -stdlib=libc++ (instead of the default: -stdlib=libstdc++).
example:
test.cpp:
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello World!!!\n";
return 0;
}
compiling using:
$ clang++ -stdlib=libc++ -o test test.cpp
will use the header from /usr/include/c++/v1 (from llvm)
but compiling using:
$ clang++ -stdlib=libstdc++ -o test test.cpp
# or (assuming the default on your system is libstdc++)
$ clang++ -o test test.cpp
will use header from /usr/include/c++/4.8 (from g++)

g++ 'stod' was not declared in this scope [duplicate]

This question already has answers here:
cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std
(5 answers)
Closed 6 years ago.
I am trying to compile the following code using g++ (version 4.8.1) in cygwin, and it seems that it cannot use the function stod():
//test.cpp
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
string a="1.23";
cout<<stod(a);
return 0;
}
I keep getting this eroor:
test.cpp:9:14: error: 'stod' was not declared in this scope
cout<<stod(a);
I read another thread that has the same problem. There, people suggested to use c++11 to address it. So I tried both the following commands to compile it but still got the same error:
g++ -std=c++0x test.cpp -o test
g++ -std=c++11 test.cpp -o test
Does anyone know a solution to this problem?
And how do I know that c++11 is enabled? Do I need to modify my code in order to use it?
Thanks a lot!
It works in GCC 4.8 on Coliru (http://coliru.stacked-crooked.com/a/8a68ad0ca64c1bff) and also in Clang on my machine. It could be that somehow your Cygwin system doesn't support this function. I suggest you work around it by simply using good old strtod() instead. That's probably what stod() uses under the hood anyway.

std::stod is not a member of std [duplicate]

This question already has answers here:
Problems with std::stoi, not working on MinGW GCC 4.7.2
(2 answers)
Closed 6 years ago.
I can't compile the following code
auto test = 42.02;
double right = std::stod(stck.top());
I'm using Code::Blocks and activated the build option to follow c++11 standard. The compiler does not complain about the auto declaration and compiles, when I put the line below in comments.
I included the string header. I'm not using a namespace.
I have no idea why this does not compile. Please help me!
edits:
My compiler is Standard MinGW GCC 4.9
For simplicity reasons, I tried the following: compiled with -std=c++11
#include <string>
int main(){
double pi = std::stod("3.14");
return 0;
}
I get the following error:
error: stod is not a member of std.
std::stod is only available if you are at least using std=c++11 to compile. Therefore, when you compile, just add the flag -std=c++11 and you will be able to use stod
Seems like you've most likely misspelled std::strtod()
You'll also need to
#include <cstdlib>

C++ #include <iostream>

I'm a complete newbie to C++. I'm trying to write a simple c++
program but I got an error message. I suspect this is due to me
accidentally deleting some .h files on my mac which might have ruined
my Clang compiler. How can I fix this? Do I need to reinstall Xcode or
change a compiler?
Error message from terminal:
192:desktop ivanlee$ gcc test.cpp
In file included from test.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:38:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:90:
In file included from /usr/include/wchar.h:70:
In file included from /usr/include/_types.h:27:
/usr/include/sys/_types.h:32:10: fatal error: 'sys/cdefs.h' file not found
#include <sys/cdefs.h>
^
1 error generated.
Code:
#include <iostream>
int main(){
return 0;
std::cout << "Hey";
}
At first I ought to recommend you the definitive stackoverflow c++ books list. Read these books and get your skills grow. This also will prevent questions like this.
Your question needs very-very basic knowledges and answers may be too long and your problem can be solved by many different methods.
I can tell you one but you should not ask questions like this.
Answer
Your code contains a mistakes:
return 0 before your other instructions (it should be the last). Now your program will just do nothing.
You should always compile C++ code with C++ compiler. gcc is not a c++ compiler but c compiler - use g++ instead.
Even if you correct 2 errors above, your std::cout call may fail because it does not flush the stream. You should also add << std::endl to this call.
Execute
g++ test.cpp -o out
Instead of
gcc text.cpp
run Terminal
execute gcc -v
Read the info and copy the include path. Copy it to your IDE that allows you to add the include path.
Mine, for an example, is:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1