tags:

views:

90

answers:

3

i have this code

struc MyItem
{
var value
MyItem* nextItem
}

MyItem item = new MyItem;

And I get the error:

"c++ cpp conversion from 'myItem*' to non-scalar type 'myItem' requested"

Compiling with g++

Any ideas?

Thanks!

+2  A: 

Try:

MyItem * item = new MyItem;

But do not forget to delete it after usage:

delete item;
tibur
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
@kralco: No, it doesn't matter. Do you have an introductory C++ book?
GMan
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
Sorry, If i didn't make myself clear, I tried MyItem *item = new MyItem and still got and error.
kralco626
@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.
GMan
@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
+2  A: 

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
A: 

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;
ArunSaha
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