atoi()给我这个错误:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast从这一行开始: int pid = atoi( token.at(0) );其中token是一个向量
我怎么才能绕过这个问题呢?
发布于 2008-10-11 05:50:13
您必须创建一个字符串:
int pid = atoi(std::string(1, token.at(0)).c_str());..。假设该标记是char的std::vector,并使用std::string的构造函数,该构造函数接受单个字符(以及字符串将包含的字符的编号,在本例中为1)。
发布于 2008-10-11 15:43:17
您的示例是不完整的,因为您没有说出向量的确切类型。我假设它是std::vector (可能是用C字符串中的每个字符填充的)。
我的解决方案是在char *上再次转换它,这将给出以下代码:
void doSomething(const std::vector & token)
{
char c[2] = {token.at(0), 0} ;
int pid = std::atoi(c) ;
}请注意,这是一个类似C的解决方案(即,在C++代码中相当难看),但它仍然很有效。
发布于 2012-02-20 22:41:47
const char tempChar = token.at(0);
int tempVal = atoi(&tempChar);https://stackoverflow.com/questions/193715
复制相似问题