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

kralco626 picture kralco626 · Oct 13, 2010 · Viewed 92.4k times · Source

I have this C++ code:

#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?

Answer

tibur picture tibur · Oct 13, 2010

Try:

MyItem * item = new MyItem;

But do not forget to delete it after usage:

delete item;