32

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
kralco626
  • 8,456
  • 38
  • 112
  • 169

4 Answers4

43

Try:

MyItem * item = new MyItem;

But do not forget to delete it after usage:

delete item;
tibur
  • 11,531
  • 2
  • 37
  • 39
  • I tried *item. think the space in between matters? The linux servers just crashed to I'll have to wait for them to come back up before I can try it with the space between then * and item. – kralco626 Oct 12 '10 at 23:43
  • @kralco: No, it doesn't matter. Do you have an introductory C++ book? – GManNickG Oct 13 '10 at 00:03
  • No. I'm taking a operating systems class. Ive taken a ton of programming classes in Java and used C# and still at work extensively. however neither of those languages use pointers like C/C++. This is just part of a larger project with implementing a program that does pipelining, (even though linux has it built in). i have the whole thing written and I get just this error. – kralco626 Oct 13 '10 at 00:12
  • Sorry, If i didn't make myself clear, I tried MyItem *item = new MyItem and still got and error. – kralco626 Oct 13 '10 at 00:12
  • 1
    @kralco: Post your real code in the question, then. You should get [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you want to program in C++, it's not going to happen by guessing. – GManNickG Oct 13 '10 at 00:18
  • @kralco If you got an error when trying `MyItem *item` then it should have been different than the first error. What did it say? – TheUndeadFish Oct 13 '10 at 02:09
30

You've mixed

MyItem item;

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

and

MyItem * item = new MyItem;

which allocates an instance of MyItem on 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.

Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
8

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

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:

MyItem* item = new MyItem;

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

Alternatively, you can create the object in the stack without the new operator.

MyItem item;

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

Leonardo Raele
  • 2,400
  • 2
  • 28
  • 32
3

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 to do new. You can possible create a MyItem object on the stack as

MyItem anotherItem;
Arun
  • 19,750
  • 10
  • 51
  • 60
  • ya, but the requirement is that we use new :(. As soon as the linux servers that crash about 30 minutes ago come back up i'll try it out. – kralco626 Oct 13 '10 at 00:13