Conversion from 'Myitem*' to Non-Scalar Type 'Myitem' Requested

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

Try:

MyItem * item = new MyItem;

But do not forget to delete it after usage:

delete item;

What does conversion to non-scalar error means? How can I resolve it?

new gives you a pointer to the newly allocated memory. The error is telling you that you can't assign a Studente* to a Studente, i.e. you need to use:

Studente* s = new Studente(...)

Or better yet, just allocate the object on the stack since you don't need heap memory in this case:

Studente s{nome, quantematerie, nomimaterie};

Or if you insist on using the heap, use smart pointers for better semantics, automatic cleanup, etc.

Error: Conversion to non-scalar type

The proper syntax would be Business bs1(1,businessName1);. If you want to use =, you can also use copy intialization Business bs2 = Business(2,businessName2);.

The former is known as direct initialization. They aren't exactly the same thing though, see Is there a difference in C++ between copy initialization and direct initialization? for in-depth information.

In Business bs1 = (1,businessName1); the 1 and array businessName1 are separated by the comma operator. The comma operator evaluates the first operand, i.e. 1 and throws away the results and returns the value of the second operand, which is an array in your case. In other words, your code is the equivalent of Business bs1 = businessName1;. This is why the error message says it cannot convert a char[10] to a Business object.

Conversion Error in C++

The value returned by the operator new has type binaryTree *. So it can be assigned to an object of this type:

binaryTree *bt = new binaryTree(root);

To call a methof for this pointer you have to use operator ->. For example

bt->deleteTree();

Or you should dereference this pointer

( *bt ).deleteTree();

The other way is to use a reference to the allocated object. For example

binaryTree &bt = *new binaryTree(root);

//...

delete &bt;

Or

bt.deleteTree();
delete &bt;


Related Topics



Leave a reply



Submit