tags:

views:

97

answers:

4

hello, i'm working on eclipse inside ubuntu envaironment on my c++ project.

i use itoa function (that works perfectly on visual studio) and the compiler shouts that itoa is undeclared.

in included <stdio.h>, <stdlib.h>, <iostream> and nothing help.

can someone please help me with this problem

thanks allot.

A: 

Did you include stdlib.h? (Or rather, since you're using C++, cstdlib)

Martin Törnwall
yes, i have tried both..
+3  A: 

itoa() is not part of any standard so you shouldn't use it. There's better ways, i.e...

C:

int main() {
    char n_str[10];
    int n = 25;

    sprintf(n_str, "%d", n);

    return 0;
}

C++:

using namespace std;
int main() {
    ostringstream n_str;
    int n = 25;

    n_str << n;

    return 0;
}
David Titarenco
actually i need to append an int to a string.
so, can i do it not with ostringstream, because my compiler does not recognize it.
+2  A: 

www.cplusplus.com says:

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

Therefore, I'd strongly suggest that you don't use it. However, you can achieve this quite straightforwardly using stringstream as follows:

stringstream ss;
ss << myInt;
string myString = ss.str();
Robin Welch
thanks,i tried it in visual studio and it does not recognize it,do i have to include somthing ?
Error 4 error C2079: 'streamstringKey' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>' h:\workspace\hw5\hw5\vehicle.cpp 151
Error 8 error C2228: left of '.str' must have class/struct/union h:\workspace\hw5\hw5\vehicle.cpp 154Error 6 error C2297: '<<' : illegal, right operand has type 'char *const ' h:\workspace\hw5\hw5\vehicle.cpp 153const string Vehicle::GetKey() const{ stringstream streamstringKey; streamstringKey << GetTypeNum(); streamstringKey << m_licenseId; //m_licenseId is char* string stringKey = streamstringKey.str(); return streamstringKey;}
#include <sstream>
Matteo Italia
+3  A: 

Boost way:

string str = boost::lexical_cast<string>(n);

dimba