将整数转换为字符串 [英] Converting integers to strings

查看:93
本文介绍了将整数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string stringAdd = (string) addToNext;

for (unsigned int i = stringAdd.length(); i>0; --i)
{
    endvec.push_back((int)(stringAdd.at(i)-'0'));
}





我不知道为什么这不起作用。



我收到此错误代码:





I am not sure why this doesn't work.

I am getting this error code:

error: invalid conversion from 'int' to 'const char*' [-fpermissive]





我不确定const char是什么意思?这是否意味着c风格的字符串(字符数组)?



I am not sure what is meant by const char? Does that mean c-style string (array of chars)?

推荐答案

请阅读错误消息:



1.该消息明确指代一行代码。你通过不检查和提供这些信息让我们和你自己变得更难:我们将不得不猜测错误信息指的是代码的哪一部分



2.提到的(第二种)类型不是 const char ,它是 const char * ,意味着指向恒定的性格。它可以引用char数组或C风格的字符串。在任何情况下,错误消息都表明它正在尝试查找到此类型的转换,即。即存在对与此类型兼容的变量的赋值,或者期望与此类型兼容的参数的函数调用。看看你的代码,这可能是指第一行或第五行。



3.提到的第一种类型是 int ,因此它可以引用任何int类型的变量或文字,或者结果类型为int的表达式。在您的代码中,这可能是指您在代码的第五行中显式转换为 int 的值。或者它可能引用第一行,如果变量 addToNext 的类型为 int



4.错误的原因是在预期不同类型的地方使用不适当类型的变量或表达式,并且没有可用的转换。在您的代码的两行中可能导致错误的是您正在使用C风格的类型转换。但由于有问题的转换是在不兼容的类型之间,所以没有演员可以解决这个问题!问题出现了为什么你首先使用这些演员阵容?有可能你并不真正理解什么类型的演员阵容:如果两行语义错误我都不会感到惊讶。



解决方案:

a)阅读类型和演员表,特别是新型演员操作符 static_cast 。不要在(或)C ++类上使用C风格的强制转换!关于此的高级读物是 http://www.gotw.ca/gotw/017.htm [ ^ ],但你可以找到每个新的教程和讨论在网络上的任何地方投射操作员,以及各种难度(和可理解性)。



b)重新考虑代码的目的,每个变量应该包含的内容,如果你实际上为每个变量使用了正确的类型。如果你认为你需要一个类型转换,很可能你的代码是错误的,或者至少你为你的变量选择了错误的类型!



c处理你的代码,直到你没有类型转换。一旦你成功完成了这项工作,你很有可能真正了解当你传递价值时发生的事情。



d)你可能会发现你可以'即使考虑到b)和c),也要做你想要的(没有类型演员)。如果你到达这一点,请回到a)并指出要真正理解类型转换可以做什么。 什么是演员不能做什么!提示:您需要的转换不存在,因此类型转换无效。相反,你需要一个转换功能(见解决方案1中的链接)



e)养成从不使用类型转换的习惯!至少从不使用C风格的类型转换。根据我的经验,类型转换几乎总是代码不好的标志。有时他们是必要的。但大多数时候,你不需要施展任何东西。如果您认为无法避免,那么代码中的某些内容可能就错了!或者,就像在这种情况下,你试图完成那些根本无法正常工作的东西。
Please read the error message:

1. the message explicitely refers to a line of code. You're making it harder on us and yourself by not checking and providing that info: we'll have to guesss what part of the code the error message is referring to

2. The (second) type mentioned isn't const char, it is const char*, meaning pointer to constant character. It may refer to a char array or C-style string. In any case the error message states it is trying to find a conversion to this type, i. e. there is an assignment to a variable compatible with this type, or a function call expecting an argument compatible to this type. Looking at your code this may refer to either the first or the fifth line.

3. the first type mentioned is int, so it may refer to any variable or literal that is of type int, or an expression that has the result type of int. In your code this may refer to the value that you explicitly cast to int in the fifth line of your code. Or it may refer to the first line, if the variable addToNext is of type int.

4. The cause of the error is the use of a variable or expression of inappropriate type, in a place where a different type is expected, and no conversion available. In both lines of your code that could have caused the error you are using C-style type casts. But since the conversion in question is between incompatible types, no cast will remedy the situation! The question arises why you did use those casts in the first place? Chances are that you don't really understand what type casts do: I wouldn't be surprised if both lines are semantically wrong.

Solution:
a) Read up on types and casts, and specifically on the new style casting operator static_cast. Don't ever use C-style casts on (or to) C++ classes! An advanced read on this is http://www.gotw.ca/gotw/017.htm[^], but you can find tutorials and discussions on each of the new casting operators anywhere on the web, and at all levels of difficulty (and comprehensibility).

b) rethink the purpose of your code, what each variable is supposed to hold, and if you are actually using the correct type for each variable. If you think you need a type cast at all, chances are your code is wrong, or at the very least you've chosen the wrong type for your variables!

c) work on your code until you can do without type casts. Once you succeed in doing that, there is a good chance you actually understand what is going on as you pass the values around.

d) You may find that you can't do what you intended (without type casts), even after considering b) and c). If you arrive at this point, go back to a) and make a point to truly understand what a type cast can do. And what a type cast can not do! Hint: the conversion you need does not exist, so a type cast won't help. Instead you need a conversion function (see link in solution 1)

e) Make it a habit to never use type casts! At the very least never use C-style type casts. In my experience, type casts are almost always a sign of bad code. Sometimes they're necessary. But most of the time, you should not need to cast anything. If you think you can't avoid it, then likely something in your code is wrong! Or, as in this case, you're trying to accomplish something that simply doesn't work that way.


检查出来:将数字转换为字符串,将字符串转换为数字 [ ^ ]


试试这个



Try this

string stringAdd = (string) addToNext;
 
const char *ptrChar = null;
for (unsigned int i = stringAdd.length(); i>0; --i)
{
    ptrChar = stringAdd.at(i);
    char tmp = *Char - '0';
    endvec.push_back((int)tmp);
}


这篇关于将整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆