Thursday, July 30, 2009

Error in Linux programming, undefined refrence to 'pow'?

I wrote a c program in linux :





#include %26lt;math.h%26gt;


#include %26lt;string.h%26gt;





int str2int(char str[])


{


int m,i,,k=0;


m=strlen(str)-1;


for(i=0;i%26lt;=l;i++)


k=k+((int) str[m-i]-48)*(int) pow(10,i);


return(k);


}





but when compile with gcc promt me:


/tmp/cciDKLpD.o(.txt+0x57): In function 'str2int' :


:undefined refrence to 'pow'


collected2: ld returned 1 exit status.





Why this error occured?

Error in Linux programming, undefined refrence to 'pow'?
you may need to compile with the -lm compiler directive....





so compile like this








gcc source.c -lm
Reply:i prefer the method below for converting strings to ints in C++,


what i can see from your code is an un-declared array (char str[]) and from my research it looks like your missing the following header file: #include %26lt;sstream%26gt;





my way:


#include %26lt;sstream%26gt;


#include %26lt;string%26gt;


using namespace std;





// string to int


string some_string;


istringstream buffer(some_string);


int some_int;


buffer %26gt;%26gt; some_int;





// int to string


int some_int;


ostringstream buffer;


buffer %26lt;%26lt; some_int;


string some_string = buffer.str();


No comments:

Post a Comment