简体   繁体   English

错误C2440:“正在初始化”:无法从“类名*”转换为“类名”

[英]error C2440: 'initializing' : cannot convert from 'classname *' to 'classname'

I have a class defined called extBlock. 我有一个定义为extBlock的类。

I then make an instance of that class with this 然后我用这个做一个该类的实例

extBlock mainBlock = new extBlock(1, 1024);

I get this error: error C2440: 'initializing' : cannot convert from 'extBlock *' to 'extBlock' 我收到此错误:错误C2440:“正在初始化”:无法从“ extBlock *”转换为“ extBlock”

Can anyone help me with why I am getting this error. 任何人都可以帮助我,为什么我会收到此错误。

I have seen examples online of declaring it like this with a pointer 我在网上看到了用指针声明它的例子

extBlock *mainBlock = new extBlock(1, 1024);

But if I do it this way it does not let me call the functions of mainBlock 但是,如果我这样做,就不会让我调用mainBlock的功能

Read up on your C++ syntax: 阅读您的C ++语法:

extBlock mainBlock(1, 1024); // create a class instance (object) on the stack
mainBlock.memberFunction(); // call a member function of a stack object

extBlock * ptrBlock = new extBlock(1, 1024); // create an object on the heap
ptrBlock->memberFunctions(); // member access through pointers has different syntax
delete ptrBlock; // must deallocate memory when you're done with a heap object

Switching from Java/C#? 从Java / C#切换?

In C++, to initialize an object on stack, you just need to use 在C ++中,要初始化堆栈上的对象,只需使用

extBlock mainBlock (1, 1024);
...
mainBlock.call_func(1,2,4,7,1);

The new operator creates an object on heap, and return the pointer to it. new运算符在堆上创建一个对象,并返回指向它的指针。 To access functions from a pointer, you need to dereference it with * : 要从指针访问函数,您需要使用*取消引用:

extBlock* mainBlock = new extBlock(1,1024);
...
(*mainBlock).call_func(1,2,4,7,1);

In C and C++, a->b can be used in place of (*a).b : 在C和C ++中,可以使用a->b代替(*a).b

mainBlock->call_func(1,2,4,7,1);

Also, C++ doesn't have Garbage Collection by default, so you need to deallocate with delete explicitly: 另外,C ++默认情况下没有垃圾收集,因此您需要使用delete显式取消分配:

delete mainBlock;

This isn't C#: new extBlock returns a pointer to an extBlock , and you're trying to assign that pointer to a value type (which would be an incompatible cast). 这不是C#: new extBlock返回指向new extBlock的指针,而您正在尝试将该指针分配给值类型(这将是不兼容的extBlock )。

What you want to write here is 你想在这里写的是

extBlock mainBlock(1, 1024);

And the reason you couldn't call methods on the second code snippet was probably because you were using the . 您无法在第二个代码段上调用方法的原因可能是因为您使用. operator instead of the -> ( arrow ) operator needed to dereference a pointer. 取消引用指针所需的运算符,而不是->箭头 )运算符。

You want this, like you had: 您想要这样,就像您曾经那样:

extBlock *mainBlock = new extBlock(1, 1024);

but then you call functions using -> instead of . 但是随后您使用->而不是调用函数. , like this: , 像这样:

mainBlock->FunctionOnIt(...);

Don't forget to delete it when it's no longer needed. 当不再需要它时,请不要忘记删除它。

delete mainBlock;

new returns a pointer to the allocated memory, where the constructor has initialized your object. new返回指向分配的内存的指针,构造函数已在其中初始化了您的对象。 Thus you need to use extBlock *mainBlock = new extBlock(1, 1024); 因此,您需要使用extBlock *mainBlock = new extBlock(1, 1024); . You can call the methods afterwards via mainBlock->someMethod() or (*mainBlock).someMethod() . 之后可以通过mainBlock->someMethod()(*mainBlock).someMethod()调用方法。

The new keyword always assigns to a pointer. new关键字始终分配给一个指针。 What you need to do is something like this: 您需要做的是这样的:

extBlock *mainBlock = new extBlock(1, 1024);
mainBlock->functionName();

Since mainBlock is now a pointer, the . 由于mainBlock现在是一个指针,因此. operator won't work anymore to reference fields or methods and the -> operator must be used in its place. 运算符将不再工作以引用字段或方法,并且->运算符必须在其位置使用。

暂无
暂无

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

相关问题 错误C2440:“正在初始化”:无法从“ LPVOID”转换为“ UINT” - error C2440: 'initializing' : cannot convert from 'LPVOID' to 'UINT 错误 C2440:“正在初始化”:无法从 - error C2440: 'initializing' : cannot convert from 错误 C2440:“正在初始化”:无法从“CTable”转换为“CTable” - Error C2440: 'initializing': cannot convert from 'CTable' to 'CTable' C2440:“正在初始化”:无法从“A”转换<double> '到'一个<double> '</double></double> - C2440: 'initializing': cannot convert from 'A<double>' to 'A<double>' 错误 3 错误 C2440:“正在初始化”:无法从“void *”转换为“Socket *” - Error 3 error C2440: 'initializing' : cannot convert from 'void *' to 'Socket *' C ++错误C2440:“正在初始化”:无法从“类名”转换为“相同的类名” - C++ error C2440: 'initializing': cannot convert from 'class name' to 'same class name' 错误C2440:“正在初始化”:无法从“ std :: _ A_iterator &lt;_B&gt;”转换为“ std :: _ A_iterator &lt;_B&gt;” - error C2440: 'initializing' : cannot convert from 'std::_A_iterator<_B>' to 'std::_A_iterator<_B>' 错误C2440:“正在初始化”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“类型*” - error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'type *' 错误C2440:“正在初始化”:无法从“初始化列表”转换 - error C2440: 'initializing' : cannot convert from 'initializer-list' 错误 C2440:“正在初始化”:无法从“const char *”转换为“TCHAR *” - error C2440: 'initializing': cannot convert from 'const char *' to 'TCHAR *'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM