Xcode 分配给强制转换是非法的,不支持左值强制转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14683007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode assignment to cast is illegal, lvalue casts are not supported?
提问by user2038971
Why am I getting this error message? On my old compiler it compiles fine but on my new compiler it gives me an error:
为什么我会收到此错误消息?在我的旧编译器上它编译得很好,但在我的新编译器上它给了我一个错误:
error: assignment to cast is illegal, lvalue casts are not supported
错误:强制转换的赋值是非法的,不支持左值强制转换
The code it points to is this line:
它指向的代码是这一行:
*((PWORD)pbyTmp)++ = (WORD)(((a_pw[0][i] * a_dX[0]) + (a_pw[1][i] * a_dX[1])) * a_dY[0] + 0.5);
回答by Lightness Races in Orbit
Your "new compiler" is correct. This is not valid C++.
您的“新编译器”是正确的。这不是有效的 C++。
The cast to (PWORD)
creates a temporary and results in an rvalueexpression, to which you shall not assign anything; the operator ++
assigns something.
强制转换(PWORD)
创建一个临时的并产生一个右值表达式,你不能给它赋值;操作员++
分配一些东西。
Put a nice, friendly, lvalueexpression (i.e. a named variable) on the left-hand-side of the assignment.
在赋值的左侧放置一个漂亮、友好的左值表达式(即命名变量)。
回答by ecatmur
The problem isn't the (syntactic) assignment; it's the postincrement (which of course includes assignment in its semantics).
问题不在于(语法)赋值;它是后增量(当然在其语义中包括赋值)。
It's possible that the intended effect of the code is:
代码的预期效果可能是:
*((PWORD)(pbyTmp++)) = ...
that is, moving the postincrement inside the cast expression.
也就是说,在强制转换表达式中移动后增量。
On the other hand, your old compiler maybe discarding the postincrement, leading to:
另一方面,您的旧编译器可能会丢弃后增量,导致:
*((PWORD)pbyTmp) = ...
To tell which of these is intended and correct, you'll have to either sufficiently understand the intent of the code (which is non-obvious from the fragment you've provided), or inspect the assembly output of the old compiler to see how it interpreted the code.
要判断其中哪些是预期的和正确的,您必须充分理解代码的意图(从您提供的片段中并不明显),或者检查旧编译器的汇编输出以了解如何它解释了代码。
回答by Mike Seymour
The problem is:
问题是:
((PWORD)pbyTmp)++
You may hope that this increments pbyTmp
to point at the next WORD
. Instead, the increment would be applied to a temporary object resulting from the conversion, leaving pbyTmp
unchanged. However, to prevent subtle bugs from mistakes like this, the language doesn't allow temporaries to be used where lvaluesare required, hence the error.
您可能希望这会增加pbyTmp
指向下一个WORD
. 相反,增量将应用于转换产生的临时对象,保持pbyTmp
不变。然而,为了防止像这样的错误造成微妙的错误,语言不允许在需要左值的地方使用临时变量,因此错误。
Presumably, your older compiler didn't diagnose the error correctly, and gave some kind of undefined behaviour instead.
据推测,您的旧编译器没有正确诊断错误,而是给出了某种未定义的行为。
You should instead put the converted pointer into a variable of the correct type:
您应该将转换后的指针放入正确类型的变量中:
PWORD * pword = reinterpret_cast<PWORD>(pbyTmp);
*pword++ = whatever;
pbyTmp = reinterpret_cast<WHATEVER_pbyTMP_IS>(pword);
or, if you really hate whoever will have to maintain your code, cast to a (lvalue) reference:
或者,如果您真的讨厌必须维护您的代码的任何人,请转换为 ( lvalue) 引用:
*((PWORD&)pbyTmp)++ = whatever;
or, better still, change the type of pbyTmp
to the correct type for what it points to.
或者,更好的是,将 的类型更改pbyTmp
为它所指向的正确类型。