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
How to Compile a C++ Code in Gcc (G++) to See The Name Mangling on Overloaded Functions
Fseek Does Not Work When File Is Opened in "A" (Append) Mode
How to Reproduce Tcp Protocol 3-Way Handshake with Raw Sockets Correctly
How to Write an C/C++ Application That Writes to a /Var/Log/Myapp Directory
What's the Real Use of Using N[C-'0']
Difference Between Dbus and Other Interprocess Communications Method
Why Doesn't Gcc Support Naked Functions
Impact of Hundreds of Idle Threads
How to Take Snapshot in Linux - Programmatically C++
Using Raii to Manage Resources from a C-Style API
What Is The Correct Behavior of Pthread_Mutex_Destroy When Destroying a Locked Mutex
Capturing H264 Stream with Opencv
"If" Block Without Curly Braces Makes Subsequent "Else If" Nested