首先是代码:
template <typename T>
void do_sth(int count)
{
char str_count[10];
//...
itoa(count, str_count, 10);
//...
}但是我得到了一些编译错误,如下所示:
error: there are no arguments to ‘itoa’ that depend on a template parameter, so a declaration of ‘itoa’ must be available
error: ‘itoa’ was not declared in this scope但我确实包括了<cstdlib>。谁能告诉我出了什么问题?
发布于 2011-09-18 20:28:49
itoa似乎是一个非标准功能,并不是在所有平台上都可用。改用snprintf (或类型安全的std::stringstream)。
发布于 2011-09-18 20:22:37
它是一个非标准函数,通常在stdlib.h中定义(但它不是由ANSI-C定义的,请参见下面的注释)。
#include<stdlib.h>然后使用itoa()
请注意,cstdlib没有此功能。所以包括cstdlib也无济于事。
还请注意,this online doc说,
可移植性
此函数未在ANSI-C中定义,也不是C++的一部分,但某些编译器支持此函数。
如果它是在header中定义的,那么在C++中,如果你必须使用它作为:
extern "C"
{
//avoid name-mangling!
char * itoa ( int value, char * str, int base );
}
//then use it
char *output = itoa(/*...params*...*/);可移植的解决方案
您可以使用sprintf将整数转换为字符串,如下所示:
sprintf(str,"%d",value);// converts to decimal base.
sprintf(str,"%x",value);// converts to hexadecimal base.
sprintf(str,"%o",value);// converts to octal base.https://stackoverflow.com/questions/7461451
复制相似问题