简体   繁体   English

C ++类型转换:错误C2440:'=':无法从'short *'转换为'DCTBLOCK'

[英]C++ Type Casting: error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK'

I have this types: 我有以下类型:

typedef short DCTELEM;
typedef DCTELEM DCTBLOCK[64];

Array of last type and a pointer to a malloc'ed array of shorts: 最后一个类型的数组和一个指向malloc分配的短裤数组的指针:

DCTBLOCK MQUAD;
short * ptrArray;

I need MQUAD to bet to specific location pointed to by ptrArray; 我需要MQUAD下注到ptrArray指向的特定位置;

In CI would prolly write something like 在CI中会写类似

MQUAD = ptrArray + 3 * 2;

and have MQUAD after that pointing to a needed location, but I get 然后将MQUAD指向所需的位置,但是我得到了

error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK' 错误C2440:“ =”:无法从“短*”转换为“ DCTBLOCK”

in c++, cause I know there is a difference in a type of array and a pointer to some-type. 在C ++中,因为我知道数组类型和指向某种类型的指针之间存在差异。

Your MQUAD variable is an array, not a pointer, so you can't assign to it (thanks to Remy Lebeau's comment). 您的MQUAD变量是一个数组,而不是一个指针,因此无法分配给它(感谢Remy Lebeau的评论)。 If you declare it as: 如果您声明为:

DCTELEM *MQUAD;

then you can assign to it: 然后您可以为其分配:

MQUAD = reinterpret_cast<DCTELEM *>(ptrArray + 3 * 2); 

This is using the C++ cast syntax. 这是使用C ++强制转换语法。 You can use C cast syntax if you like too. 如果愿意,也可以使用C强制转换语法。

The main mistake is 主要错误是

DCTBLOCK MQUAD // it's wrong
DCTELEM* MQUAD // it's right

and you may cast as u want 你可以随心所欲

MQUAD = ptrArray + 3 * 2;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++嵌套类模板错误C2440&#39;=&#39;:无法从&#39;type&#39;转换为&#39;same type&#39; - C++ Nested class template Error C2440 '=': cannot convert from 'type' to 'same type' 错误c2440&#39;=&#39;无法从int *转换为Type <T> * - Error c2440 '=' cannot convert from int * to Type<T> * c ++:错误C2440:“ <function-style-cast> &#39;:无法从&#39;A转换 <TYPE> &#39;至&#39;B <TYPE> &#39; - c++:error C2440: '<function-style-cast>' : cannot convert from 'A<TYPE>' to 'B<TYPE>' C ++ C2440错误无法从&#39;const BWAPI :: UpgradeType&#39;转换为&#39;const BWAPI :: Type *&#39; - c++ C2440 error cannot convert from 'const BWAPI::UpgradeType' to 'const BWAPI::Type *' C ++类型转换:错误C2440:“正在初始化”:无法从“ HRESULT”转换为“ std :: basic_string &lt;_Elem,_Traits,_Alloc&gt;” - c++ type conversion: error C2440: 'initializing' : cannot convert from 'HRESULT' to 'std::basic_string<_Elem,_Traits,_Alloc>' 错误C2440:“返回”:无法从“ int [2]”转换为“ int(&amp;&amp;)[2]” - Error C2440: 'return' : cannot convert from 'int [2]' to 'int (&&)[2]' decltype 错误 C2440 无法从“int *”转换为“int *&amp;” - decltype error C2440 cannot convert from 'int *' to 'int *&' 错误 C2440:“正在初始化”:无法从“CTable”转换为“CTable” - Error C2440: 'initializing': cannot convert from 'CTable' to 'CTable' 错误C2440:“正在初始化”:无法从“ LPVOID”转换为“ UINT” - error C2440: 'initializing' : cannot convert from 'LPVOID' to 'UINT 错误C2440:“ =”:无法从“ int”转换为“ char [5]” - error C2440: '=' : cannot convert from 'int' to 'char [5]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM