C++ 请求从“myItem*”转换为非标量类型“myItem”

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

Conversion from 'myItem*' to non-scalar type 'myItem' requested

c++new-operator

提问by kralco626

I have this C++ code:

我有这个 C++ 代码:

#include <iostream>
using namespace std;
struct MyItem
{
  int value;
  MyItem* nextItem;
};

int main() {
    MyItem item = new MyItem;
    return 0;
}

And I get the error:

我得到错误:

error: conversion from `MyItem*' to non-scalar type `MyItem' requested

Compiling with g++. What does that mean? And what's going on here?

用 g++ 编译。这意味着什么?这是怎么回事?

回答by tibur

Try:

尝试:

MyItem * item = new MyItem;

But do not forget to delete it after usage:

但不要忘记在使用后删除它:

delete item;

回答by Nick Meyer

You've mixed

你混了

MyItem item;

which allocates an instance of MyItemon the stack. The memory for the instance is automatically freed at the end of the enclosing scope

MyItem在堆栈上分配了一个实例。实例的内存在封闭范围结束时自动释放

and

MyItem * item = new MyItem;

which allocates an instance of MyItemon the heap. You would refer to this instance using a pointer and would be required to explicitly free the memory when finished using delete item.

MyItem在堆上分配了一个实例。您将使用指针引用此实例,并且在使用完后需要显式释放内存delete item

回答by Leonardo Raele

First of all, this code won't compile because you forgot the semi-colons after each member variable declaration and after MyItemdeclaration and the keyword "struct" is typed wrong. Your code should look like this:

首先,这段代码无法编译,因为您忘记了每个成员变量声明之后MyItem和声明之后的分号,并且关键字“struct”键入错误。您的代码应如下所示:

struct MyItem
{
var value;
MyItem* nextItem;
};

MyItem item = new MyItem;

Now answering your question, this code does not work because the new operator returns a pointer to the object created (a value of type MyItem*) and you are trying to assign this pointer to a variable of type MyItem. The compiler does not allow you to do this (because the value and the variable have different types). You should store the pointer into an apropriate variable, like this:

现在回答你的问题,这段代码不起作用,因为 new 运算符返回一个指向创建的对象的指针(一个 type 的值MyItem*),而你正试图将此指针分配给一个 type 的变量MyItem。编译器不允许您这样做(因为值和变量具有不同的类型)。您应该将指针存储到一个适当的变量中,如下所示:

MyItem* item = new MyItem;

In this case, you must remember to delete itemto avoid memory leak once you no more need it.

在这种情况下,您必须记住在delete item不再需要时避免内存泄漏。

Alternatively, you can create the object in the stack without the newoperator.

或者,您可以在没有new运算符的情况下在堆栈中创建对象。

MyItem item;

In this case, the object ceases to exist when the function returns; you don't need to remember to delete it.

在这种情况下,当函数返回时,对象不复存在;你不需要记得删除它。

回答by Arun

Here is edited code with changes mentioned on the right

这是已编辑的代码,右侧提到了更改

struct MyItem                  // corrected spelling struct
{
    var value;                 // added ;
    struct MyItem * nextItem;  // add "struct" and added ;
};                             // added ;

MyItem * item = new MyItem;    // added * before item

delete item;                   // not exactly here, but some where in your code

BTW, you don't have todo new. You can possible create a MyItemobject on the stack as

顺便说一句,你不必须做的new。您可以MyItem在堆栈上创建一个对象作为

MyItem anotherItem;