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!
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!
Try:
MyItem * item = new MyItem;
But do not forget to delete it after usage:
delete item;
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
.
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;