29442

C++ Binary Tree error: request for member (X) in (Y) which is of non-class type (Z)

Hey all, so I am trying to build a simple binary tree that has two keys and evaluates the sum for its sorting. Here is what it's looking like:

struct SumNode { int keyA; int keyB; SumNode *left; SumNode *right; }; class SumBTree { public: SumBTree(); ~SumBTree(); void insert(int, int); SumNode *search(int, int); SumNode *search(int); void destroy_tree(); private: SumNode *root; void insert(int,int, SumNode*); SumNode *search(int,int, SumNode*); SumNode *search(int, SumNode*); void destroy_tree(SumNode*); }; SumBTree::SumBTree() { root = NULL; } SumBTree::~SumBTree(){}; void SumBTree::insert(int a, int b, SumNode *leaf) { int sum = a + b; int leafsum = leaf->keyA + leaf->keyB; if (sum < leafsum) { if (leaf->left != NULL) { insert(a,b, leaf->left); } else { leaf->left = new SumNode; leaf->left->keyA = a; leaf->left->keyB = b; leaf->left->left = NULL; leaf->left->right = NULL; } } else { if (leaf -> right != NULL) { insert(a,b, leaf->right); } else { leaf->right = new SumNode; leaf->right->keyA = a; leaf->right->keyB = b; leaf->right->left = NULL; leaf->right->right = NULL; } } } SumNode *SumBTree::search(int a, int b, SumNode *leaf) { if (leaf != NULL) { if (a == leaf->keyA && b == leaf->keyB) return leaf; int sum = a + b; int leafsum = leaf->keyA + leaf->keyB; if (sum < leafsum) return search(a, b, leaf->left); return search(a, b, leaf->right); } return NULL; } SumNode *SumBTree::search(int sum, SumNode *leaf) { if (leaf != NULL) { int leafsum = leaf->keyA + leaf->keyB; if (sum == leafsum) return leaf; if (sum < leafsum) return search(sum, leaf->left); return search(sum, leaf->right); } return NULL; } void SumBTree::destroy_tree(SumNode *leaf) { if(leaf != NULL) { destroy_tree(leaf->left); destroy_tree(leaf->right); delete leaf; } } void SumBTree::insert(int a, int b) { if (root != NULL) { insert(a,b, root); } else { root = new SumNode; root->keyA = a; root->keyB = b; root->left = NULL; root->right = NULL; } } SumNode *SumBTree::search(int a, int b) { return search(a, b, root); } SumNode *SumBTree::search(int sum) { return search(sum, root); } void SumBTree::destroy_tree() { destroy_tree(root); }

I went to test it out, using this:

#include <iostream> #include "SumBTree.h" using namespace std; int main() { cout << "Initializing Tree" << endl; SumBTree sbt(); cout << "Inserting (2,3)" << endl; sbt.insert(2,3); cout << "Hello world!" << endl; return 0; }

But whenever I try to build it I get the following error:

||=== BTree, Debug ===| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp||In function int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for memberinsert' in sbt', which is of non-class typeSumBTree ()()'| ||=== Build finished: 1 errors, 0 warnings ===|

I can't figure out what I'm doing wrong! Is it the overloaded functions? I don't get it. Does anyone here know?

Answer1:

you need to replace SumBTree sbt(); with SumBTree sbt;

Right now it thinks sbt isn't a class. If there are no parameters in a constructor don't use empty brackets.

Recommend

  • Why CTE calculation is duplicated in query plan and how to optimize it without duplicating code?
  • How to plot predictions of binomial GLM that has both continuous and categorical variables
  • Easier way to do non-recursive list on gcs bucket outside appengine
  • How to click on a link that has a certain content in puppeteer?
  • Detecting null parameter in preprocessor macro
  • CERN ROOT exporting data to plain text
  • OSX - always hide certain files
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • msbuild create itemgroup from property group
  • Elasticsearch script query involving root and nested values
  • JBoss External Properties Files in Classpath
  • Eloquent paginate function in Slim 3 project using twig
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • htaccess add www if not subdomain, if subdomain remove www
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • $wpdb not working in file of WordPress plugin
  • Meteor helpers not available in Angular template
  • Jenkins: How To Build multiple projects from a TFS repository?
  • What is Eclipse's Declaration View used for?
  • PHP - How to update data to MySQL when click a radio button
  • output of program is not same as passed argument
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Can I make an Android app that runs a web view in Chrome 39?
  • Javascript + PHP Encryption with pidCrypt
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • Getting Messege Twice Using IMvxMessenger
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?