Я сделал программу на С ++, которая использует itoa (). Я скомпилировал его на 64-битном компиляторе (TDM-GCC-5.1), он скомпилирован и работает. Но когда я скомпилировал его с использованием 32-битного компилятора TDM-GCC-5.1, я понял, что itoa () не было объявлено в этой области. Я попытался скомпилировать это на двух разных 32-битных машинах, но я получил ту же ошибку, и она включила cstdlib и stdlib.h, но в обоих случаях ошибка одна и та же.
Он компилируется и отлично работает на 64-битной машине. Но почему это не происходит на 32-битном компиляторе?
Можете ли вы предложить какую-нибудь альтернативу для 32-битной?
Содержание
- Решение
- 3 Answers 3
- 3 Answers
Решение
itoa не является стандартной функцией. Это обеспечивается некоторыми реализациями, а не другими. Избегайте этого в портативном программном обеспечении.
64-разрядная версия TDM GCC в режиме по умолчанию обеспечивает itoa, а 32-разрядная — нет. Чтобы сохранить согласованность между версиями, попробуйте, например, -std=c++11 -DNO_OLDNAMES -D_NO_OLDNAMES ,
Соответствующая стандартам альтернатива будет, например,
Говоря о портативности, main() без int Это серьезная ошибка, которая ошибочно остается без диагностики в некоторых версиях GCC для Windows. Это ошибка в этой версии GCC. Кроме того, доступ к неинициализированной переменной test вызывает неопределенное поведение.
I am getting the following error:
prog.cpp: In member function ‘void Sequence::GetSequence()’:
prog.cpp:45: error: ‘itoa’ was not declared in this scope
I have include cstdlib header file but its not working.
3 Answers 3
There’s no itoa in the standard, but in C++11 you can use the std::to_string functions.
In C++11 you can use std::to_string . If this is not available to you, you can use a std::stringstream :
If this is too verbose, there is boost::lexical_cast. There are some complaints about the performance of lexical_cast and std::stringstream , so watch out if this is important to you.
Another option is to use a Boost.Karma, a sublibrary of Spirit. It comes out ahead in most benchmarks.
Usually, itoa is a bad idea. It is neither part of the C nor C++ standard. You can take some care to identify the platforms that support it and use it conditionally, but why should you, when there are better solutions.
im using format itoa(i,string_temp, 10);
atoi is working, but not atoa, why?
yes i include stdlib and stdio. :/
im on mac, if that helps
3 Answers
There is no such thing as itoa() in C++ (or in C, for that matter).
New C++ compilers have std::to_string() which converts integral types to std::string objects, if not, try getting boost for its boost::lexical_cast (), and if not — write your own converter.
std::string str = std::to_string(123);
std::string str = boost::lexical_cast (123);
Источник: