You are here:

C++/About a function that transforms a integar into string


This is the code, and I keep getting an error which I can not understand...

char* ToString(int intdata) {
int p;
int q;
int temp = intdata, quotient, remainder;
int count = 0;
do {
  quotient = temp/10;
  temp = quotient;
while(quotient != 0);
temp = intdata;
char *aray = new char[count];
for(p=0, q=count-2; p<count-1; p++, q--) {
//[quotient = temp / pow (10, q); I get an error here.  saying implicit declaration of function 'int pow(...)']
  quotient = temp / pow(10, q);
  remainder = temp % (int)pow(10, q);
  aray[p] =  quotient+48;
  temp = remainder;
aray[p] = 0;//giving null character to last element of aray.

return aray;

Do you know the problem?  Thank you once again.

Yes. pow is not a built in feature of the core language. It is a function that is part of the language library - in fact it is part of the C language library that C++ incorporates.

As it is a function the compiler prefers that it has seen at least a declaration - sometimes called a function prototype - for the function before it is called. It could also have seen the definition - which is also a declaration of the function - but in the case of pow this will not be so as pow will be in some separate library - such as libc or some such - that is linked in with your program code once all the program source files have been compiled.

If the compiler has not seen anything to tell it the information on the function's parameters and return types then the compiler tries to work it out from the first use of the function (hence the use of the word 'implicit' in the error - the compiler is trying to imply the declaration of pow). In this case it gets it wrong. Is has assumed pow takes and returns int values when in fact the library version is declared thus:

       double pow(double x, double y);

or in C++ (taken from book "The C++ Standard"):

       double pow(double x, double y);
       double pow(double x, int y);
       float pow(float x, float y);
       float pow(float x, int y);
       long double pow(long double x, long double y);
       long double pow(long double x, int y);

- although the names x and y are probably not included in the declaration provided by the compiler provider. This is a feature of C and not C++. In C++ it is an error (the standard uses the term "ill-formed") if a call is made to a function for which no declaration is visible to the compiler - which means it must have seen one _before_ the call - this I think is necessary as in C++ - as shown above - function names can be overloaded on the number and types of the parameters they take.

OK, so you need a declaration of the pow library function(s) - how do you do this. Well the C and C++ standard libraries come with such declarations in header files. In the case of pow the function is part of the math section of the library so the declaration is placed in math.h (for C) or cmath (for standard C++ - note the lack of an extension). So all you need to do is include math.h (or cmath) in your

/* yourcode.cpp

#include <math.h>

/* ... */

char* ToString(int intdata)
/* ... */

I assume your code and compiler are more at home with C than C++ because the _only_ C++ feature or code style you have used appears to be new. Everything else looks like it has been written in the style of C rather than C++, and your compiler seems to be not up to speed on C++ either if it is trying to assume the type of an undeclared called function - so I played safe and include math.h rather than cmath in case your C++ library is not that new (note: C++ was standardised in 1998!). Notice the use of <> in the include directive - this is because math.h is part of the language 'system' and counts as a system header file rather than a local project header file - in which cases you replace < and > with " and " (double quotes). Exact usage between these two quoting styles varies between compilers and/or pre-processors (this part of the compilation is a stage called pre-processing - it is done before compilation proper starts - and on some systems is done by a separate program - on UN*X like systems it is called cpp - C Pre Processor - or similar).  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.