类型铸造/转换 [英] type casting / conversion

查看:87
本文介绍了类型铸造/转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有人以最原始的方式解释,如何在类型之间进行转换或转换,它们之间的区别是什么,我何时使用哪个,以及为什么我一直不能从类型转换为类型,以及我怎么做到最佳,


i意味着所有我需要的是字符串,char,char数组,浮点数,double等之间的标准cionversions,但我不断得到错误它让我疯狂!


提前感谢,

解决方案

我假设您正在使用C ++(现在看起来有点明显,但您应该提一下你使用什么语言)。您可以使用五种不同的铸造方法。第一个是旧的C风格演员表,你应该避免使用它。其他四个是C ++样式转换,您应该使用它们。我不想告诉你它们是什么,因为这只是谷歌搜索和自己阅读信息的问题。


事实上,你的整个问题实际上是要求我们谈论铸造的话题。这不再是一个问题。它是一个有意义的问题,什么是C ++字符串并告诉我关于它们的一切。阅读有关在Google上进行C ++投射的内容。或者像Lippman这样的C ++ Primer这本好书。


请记住,施法不涉及任何逻辑。将字符串转换为int并不意味着您将从字符串转换为int。强制转换是一种覆盖编译器的方法。你有效地说,嘿,我知道我在这里做什么,把这个变量视为另一个。是否有意义是未知的。这取决于你知道你在做什么。


抱歉我问这个问题不专业的方式,但问题是我已经阅读了很多关于铸造,我知道它是如何工作的以及它做了什么,但是我一直在遇到错误,我可能会遇到某种障碍,但我只是没有得到它,如果我可以在给我错误的那一刻说明我的例子,


i有来自strtok函数的* char,我需要将其作为Maya'API中的方法的参数传递,作为双精度



1> d:\ maya work \ api projects(c ++)\ retrievegeocmd \retrievegeocmd \retrievegeocmd cmd.cpp(164):错误C2440:''static_cast'':无法转换从''char *''到''double''


i尝试了各种铸造方式,它不起作用,


再次感谢你的时间



i有一个* char来了来自strtok函数,我需要将其作为Maya'API中的方法的参数传递,作为双重


1> d:\ maya work \ api项目(c ++)\ retrievegeocmd \retrievegeocmd \retrievegeocmd cmd.cpp(164):错误C2440:''static_cast'':无法从''char *''转换为''double''



这就是oler1s所说的,你的第一个问题非常通用且难以回答,但这是非常具体的(这是一个问题)我们可以回答它(虽然发布产生错误的代码行也会更好。)


好​​的简短答案是编译器无法在char *和double之间进行转换。它们是不兼容的类型,编译器只是不知道如何将指针中的位视为双值。


但是我认为你使用了错误的方法解决问题。我猜你有一串代表双值的字符,你想在double类型的变量中得到这个值。


铸造不是这样做的方法,铸造用于类似类型的变量之间,因此您可以在所有数值类型(char,int,long,float double等)之间进行转换,并且可以在某些指针类型之间进行转换,例如任何指针转换为void *,你可以在类层次结构中上下转换指针。


你需要将字符串(字符数组)转换为double。在C ++中,最简单的方法是将chars数组导入stingstream类型变量并将其导出为双精度

展开 | 选择 | Wrap | 行号

i need someone to explain in the most primitive way possible, how to cast or convert between types, whats the difference between them, when do i use which, and why do i keep getting the cannot convert from type to type, and how would i do it optimally,

i mean all i need is standard cionversions between string, char, char array, float, double, etc, but i keep getting errors its driving me crazy!

thanks in advance,

解决方案

I assume you are using C++ (seems a bit obvious now, but you should mention what language you use). There''s five different casting methods available to you. The first is the old C style casts, which you should avoid using. The other four are C++ style casts, which you should use. I won''t bother to tell you what they are as this is simply a matter of googling and reading information on your own.

In fact, your entire question is really asking us to talk about the topic of casting. That''s no longer a question. It''s as meaningful a question as "what are C++ strings and tell me everything about them". Read up about C++ casting on Google. Or a good book like C++ Primer by Lippman.

Remember that casting does not involve any logic. Casting a string to an int does not mean you get a conversion from a string to an int. Casting is a way of overriding the compiler. You effectively say, hey I know what I''m doing here, treat this variable like another. Whether it is meaningful or not is unknown. It''s up to you to know what you''re doing.


sorry for the unprofessional way i asked the question, but the problem is i have read a lot about casting, i know how it works and what it does, but i keep getting errors, i might be having some kind of a block, but im just not getting it, if i may state the example im in at the moment that giving me the errors,

i have a *char coming from the strtok function, which i need to pass as an argument for a method in Maya''s API, as a double


1>d:\maya work\api projects (c++)\retrievegeocmd\retrievegeocmd\retrievegeocmd cmd.cpp(164) : error C2440: ''static_cast'' : cannot convert from ''char *'' to ''double''

i tried every way of casting, and its not working,

thanks again for your time


i have a *char coming from the strtok function, which i need to pass as an argument for a method in Maya''s API, as a double


1>d:\maya work\api projects (c++)\retrievegeocmd\retrievegeocmd\retrievegeocmd cmd.cpp(164) : error C2440: ''static_cast'' : cannot convert from ''char *'' to ''double''

This is what oler1s was talking about, your first question was very generic and hard to answer, this however is quite specific (it is a single problem) and we can answer it (although posting the code lines that produce the error as well would have been better).


OK the short answer is that the compiler just can not cast between char * and double. They are incompatible types, the compiler just does not know how the treat the bits in the pointer as a double value.

However I think you are using the wrong approach to your problem. I am guessing you have a string of characters that represent a double value and you want to get that value in a variable of type double.

Casting is not the way to do this, casting is for use between variables of similar type so you can cast between all the numerical types (char, int, long, float double etc.) and you can cast between some pointer types e.g. any pointer casts to void *, you can cast pointers up and down class hierarchys.

You need to convert a character string (array of chars) into a double. In C++ the easiest way to do this is to import your array of chars into a stingstream type variable and the export them out as a double

Expand|Select|Wrap|Line Numbers


这篇关于类型铸造/转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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