[英]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.