Compilation error while using to_string in c++ program - c++

To get precision and scale of a number i am using this simple program. But while converting number into string it is giving compilation error.
g++ precision.cpp
precision.cpp: In function ‘int main()’:
precision.cpp:6: error: ‘to_string’ was not declared in this scope
When I compile with the -std=c++0x switch I get
g++ precision.cpp -std=c++0x
precision.cpp: In function ‘int main()’:
precision.cpp:6: error: call of overloaded ‘to_string(int)’ is ambiguous
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/basic_string.h:2604: note: candidates are: std::string std::to_string(long long int)
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/basic_string.h:2610: note: std::string std::to_string(long long unsigned int)
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/basic_string.h:2616: note: std::string std::to_string(long double)
The source code looks like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string value = to_string(static_cast<int>(1234));
int precision = value.length();
int scale = value.length()-value.find('.')-1;
cout << precision << " " << scale;
return 0;
}
What is causing this error?

The first error is because std::to_string is a C++11 feature, and GCC by default compiles in C++03 mode.
The second error, when you are using the correct flag, is probably because the support for C++11 in GCC 4.4 (which you seem to be using) is quite minimal. As you can see by the error messages, the compiler shows you the alternatives it have.
By the way, you don't need to cast integer literals to int, they are of type int by default. You might want to cast it to long double though, as that's one of the valid overloads and you seems to want to find the decimal point (the code will not work as expected if there is no decimal point in the string, like when converting an integer).

I recommend to use boost::lexical_cast instead.

Related

boost bad lexical cast: source type value could not be interpreted as target when converting a string to unsigned long long

the following piece of code that I compiled on wandbox.org is causing the following error. I don't understand why I am getting the error.
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include "boost/lexical_cast.hpp"
typedef unsigned long long Ulonglong ;
int main()
{
Ulonglong result = boost::lexical_cast<unsigned long long>("862.00");
return 0;
}
Start prog.cc: In function 'int main()': prog.cc:11:15: warning:
unused variable 'result' [-Wunused-variable] 11 | Ulonglong
result = boost::lexical_cast("862.00");
| ^~~~~~ terminate called after throwing an instance of 'boost::wrapexcept' what():
bad lexical cast: source type value could not be interpreted as target
Aborted Finish
It seems boost::lexical_cast must perform an exact conversion, with no extended behaviour. You are trying to cast a string representation of a number containing a decimal point (thus containing a fractional part) to an integer, which is not allowed.
You should either first convert to float/double (mind the data loss for very large integers) and then convert to integer, or cut off the decimal part of the string before handing it off to boost::lexical_cast.

How to use the C++ max function with boost 128 bit integer

If I have the following code:
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision
int main()
{
int128_t a = Func_a()
int128_t b = Func_b()
std::cout << std::max(a, b) << std::endl;
return 0;
}
And if I compile using g++ on Ubuntu, I get the following error:
error: cannot convert ‘const boost::multiprecision::number >’ to ‘int64 {aka long long int}’ in assignment
What is the proper way to compare two int128_t numbers to see which one is greater?
EDIT: I am using std::max.
Your code (except for missing semicolons) compiles and runs without error.
However, according to your compiler message, I'm suspecting that in
int128_t a = Func_a(); // are you really sure it is int128_t?
the left-hand side is not a boost::multiprecision::int128_t, since the compiler says it is a int64.

Pointer arithmatic failed to compile, gcc says invalid type conversion

I wish to calculate the distance between a program's bss section and the start of heap section, so I've got a program like this:
$ cat 1.cpp
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int empty;//bss
int main()
{
char*p=(char*)malloc(0);
printf("%d\n",(int)p-(int)&empty);
return 0;
}
When I compile that code using:
$ g++ 1.cpp
The errors are:
1.cpp: In function ‘int main()’:
1.cpp:8:22: error: cast from ‘char*’ to ‘int’ loses precision [-fpermissive]
printf("%d\n",(int)p-(int)&empty);
^
1.cpp:8:30: error: cast from ‘int*’ to ‘int’ loses precision [-fpermissive]
printf("%d\n",(int)p-(int)&empty);
^
I don't think it's improper to convert an int* to int. I found plenty of examples doing this on the internet.
Why is my code not compiling?
Don't always trust what you read on the Internet.
Presumably, you're on a platform where pointers are 64-bit and int is 32-bit. In this case, a conversion from a pointer to int loses precision, and you're compiling with the right flags to make this an error. Well done!
The type you're looking for to convert a pointer to an integral type is uintptr_t. This is defined by the standard to be of sufficient width to allow for lossless conversion.
You may want p-reinterpret_cast<char*>(&empty)
But I don't see any reason to calculate distance between the two address. (and is undefined behavior as noted by #mch)
The type to cast to from pointers should be uintptr_t, which is guaranteed not to lose precision, see also fixed width integer types.
If you insist on using printf, then you need to find the right formatter, because that is not fixed. Otherwise, use:
std::cout << reinterpret_cast<uintptr_t>(p);

.cpp:23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’

Here a basic code I'm trying to run But I'm having trouble with stoi (it's c++) I keep getting error:
‘stoi’ was not declared in this scope
I tried atoi and strtol with this error
.cpp:23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
The code:
using namespace std;
int main(){
string numberGuessed;
int intNumberGuessed = 0;
do {
cout << "Guess a numeber btw 1 - 10: " << endl;
getline(cin, numberGuessed);
intNumberGuessed = atoi(numberGuessed);
cout << intNumberGuessed << endl;
} while(intNumberGuessed != 4);
cout<< "you win" << endl;
return 0;
}
The atoi() function accepts const char* argument, but you're trying to pass it std::string. Write it like intNumberGuessed = atoi(numberGuessed.c_str()); to take the pointer.
As for the first error, about stoi() being undeclared — it is because the function was added in C++11 standard, so you have to enable its support in your compiler. I.e. in older versions of GCC you could do it with -std=c++11 option (since gcc5 C11 is enabled by default, and since gcc6 C++11 will be enabled by default).
Use stoi, it's the modern C++ version of C's atoi.
Update:
Since the original answer text above the question was amended with the following error message:
‘stoi’ was not declared in this scope
Assuming this error was produced by g++ (which uses that wording), this can have two different causes:
Using a non-conforming variant of g++ that doesn't provide std::stoi.
Using g++ in C++03 mode (stoi was introduced in C++11).
For Windows, the MinGW-w64 variant is known to provide std::stoi, and in particular the Nuwen distribution is based on MinGW-w64.
For C++11 mode, with g++ use the option -std=c++11. For example, this is necessary with the Nuwen distribution g++ version 5.1.

C++ Type Check in Functions ignored (required double, provided int)

I have the following code
#include <iostream>
using namespace std;
int dmult(int a, int b){
return 2*a*b;
}
int main(void)
{
double a = 3.3;
double b = 2;
int c = dmult(a,b);
cout << c << endl;
return 0;
}
It compiles with MinGW without problems. The result is (as I thought) false. Is it a problem of the compiler that there is no warning, that a function expecting integers, but fed with doubles, can compile without warning even if the input type is wrong? Does it mean that C++ ignores the input type of a function? Shouldn't it realize that the function arguments have the wrong type?
double's are implicitly convertible to ints (and truncated), and the compiler is not forced by the standard to emit a warning (it tries its best to perform the conversion whenever possible). Compile with -Wconversion
g++ -Wconversion program.cpp
and you'll get your warning:
warning: conversion to 'int' from 'double' may alter its value [-Wfloat-conversion]
The typical warning flags -Wall -Wextra don't catch it since many times it is the programmer's intention to truncate double's to int's.
Live example here.
c++ automatically casts floats and doubles to integer literals by truncating them. so 3.3 becomes 3 when you call dmult(3.3,2)