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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2654556/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:15:02  来源:igfitidea点击:

error C2440: 'initializing' : cannot convert from 'classname *' to 'classname'

c++classinstance

提问by Mike

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 的函数

回答by Nick Meyer

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

回答by kennytm

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 newoperator creates an object on heap, and return the pointer to it. To access functions from a pointer, you need to dereference it with *:

new操作创建于堆中的对象,指针返回到它。要从指针访问函数,您需要使用以下命令取消引用它*

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

In C and C++, a->bcan 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 deleteexplicitly:

此外,默认情况下 C++ 没有垃圾收集,因此您需要delete显式解除分配:

delete mainBlock;

回答by Mark Rushakoff

This isn't C#: new extBlockreturns 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返回一个指向 an 的指针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.

并且您不能在第二个代码片段上调用方法的原因可能是因为您使用了.运算符而不是->箭头)运算符需要取消引用指针。

回答by Stephen

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;

回答by swegi

newreturns a pointer to the allocated memory, where the constructor has initialized your object. Thus you need to use extBlock *mainBlock = new extBlock(1, 1024);. You can call the methods afterwards via mainBlock->someMethod()or (*mainBlock).someMethod().

new返回指向已分配内存的指针,其中构造函数已初始化您的对象。因此,您需要使用extBlock *mainBlock = new extBlock(1, 1024);. 之后您可以通过mainBlock->someMethod()或调用这些方法(*mainBlock).someMethod()

回答by Chinmay Kanchi

The newkeyword alwaysassigns to a pointer. What you need to do is something like this:

new关键字总是分配给一个指针。你需要做的是这样的:

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

Since mainBlock is now a pointer, the .operator won't work anymore to reference fields or methods and the ->operator must be used in its place.

由于 mainBlock 现在是一个指针,.运算符将不再用于引用字段或方法,并且->必须在其位置使用运算符。