| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C++ itoa function gives me errors
Hello:
This is what I am trying to do, assign an integer to a string so for instance: int myInt=5; string myStr; myStr=myInt; and I want myStr to have "5". So I am using the "itoa()" function of the format: #include <cstdlib> char* itoa( int value, char* buffer, int radix ); and I know that that I am implementing it properly. I am running two compilers, Metrowerks code warrior on windows and G++ on Linux, so when I compile the code on the windows machine it works fine but on the Linux machine (which is my first priority) I get this error: implicit declaration of function `int itoa(...)' What am I doing wrong? I googled quite a bit and found some people saying that it might be a bug, but that they werent sure. so is there a way that I can use that function in G++? Thanx! |
|
#2
|
|||
|
|||
|
Do you really need it in a variable? Why not just do:
PHP Code:
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#3
|
|||
|
|||
|
I have no problems with the use of the itoa() function. Here is the simple test code i compiled. Written in MSVisual C++
#include <iostream> #include <string> #include <cstdlib> using namespace std; void main() { int i=12; // number to be converted string s; // string to recive the number char b[50]; // buffer of chars int radix=10; // 2:bin, 8:octal, 10:dec, 16:hex s=itoa(i,b,radix); cout << "int i=" << s << endl; cout << "buffer=" << b << endl; } |
|
#4
|
|||
|
|||
|
You've included the header <cstdlib>.
That's correct, except you've forgotten that everything in cstdlib is defined in the std namespace. So, you can do the following (which I don't recommend): Code:
using namespace std; Either of the two methods below is recommended, although the first is preferred. The reason is that they don't clutter up the global namespace with std classes, functions and variables, and thus defeat the purpose of a namespace. You can specify the namespace explicitly when you call itoa(), like so (preferred): Code:
std::itoa(MyInt) Or, even this: Code:
using std::itoa // Now I don't need the full std::itoa() call, // but I haven't imported everything in std either. itoa(MyInt); Hope that sets you on track. Last edited by Jeb. : October 5th, 2003 at 09:06 PM. |
|
#5
|
|||
|
|||
|
Yes
Yes that worked. I had the same problem with printf and strncmp
got the error: implicit declaration of function int printf(...) Now it works! Thanks |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ itoa function gives me errors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|