1

I have been coding an assignment in C++ using Binary Search Trees, and while coding my "main" method, came across this error;

 91:37: error: conversion from ‘binaryTree*’ to non-scalar type ‘binaryTree’ requested

Line 91 is as follows;

 binaryTree bt = new binaryTree(root);

and I can't understand what's wrong with it, my lab tutor doesn't understand the error either.

  • spelling is definitely correct

Any help would be great - thanks!

Megan Sime
  • 1,257
  • 2
  • 16
  • 25

3 Answers3

2

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

new returns a pointer and bt is not a pointer.

 binaryTree *bt = new binaryTree(root);
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
0

You are assigning a pointer(binaryTree*) to a regular variable of type binaryTree. This cannot work.

You should instead do:

binaryTree* bt = new binaryTree(root);

KalyanS
  • 527
  • 3
  • 8