error: expression cannot be used as a function c++ [duplicate] - c++

This question already has answers here:
How to call function with same name as class member
(1 answer)
Why calling a non-member function with the same name as a member function generates an error
(4 answers)
Closed 4 days ago.
I am trying to build an AVL tree in c++ and I when trying to build a function that calculates the balance factor of a node, I get this error:
avl.cpp:34:30: error: expression cannot be used as a function
34 | return (height(this->left) - height(this->right));
This is how my code roughly looks like:
class Node {
public:
int key;
Node* left;
Node* right;
int height;
Node(int key) {
this->key = key;
this->height = 1;
}
int balanceFactor();
};
int max(int a, int b) {
a > b ? a : b;
}
int height(Node* node) {
if (node == nullptr)
return 0;
return node->height;
}
int Node::balanceFactor() {
return (height(this->left) - height(this->right));
}
I tried getting rid of the parentheses in the return statement of balanceFactor, like this: return height(this->left) - height(this->right) but I got the same error.

Your field int Node::height shadows the global function of the same name. Rename either of them. The compiler can find such errors for you if you increase the warning settings.

Related

Why I am getting different outputs with these 2 assignments, one in a function and other in main of a pointer? [duplicate]

This question already has answers here:
Function does not change passed pointer C++
(4 answers)
Closed 3 years ago.
"I was trying to assign NULL to a pointer root in a function. But it doesn't really get assigned by NULL using function. But when I tried to assign root = NULL in main it get assigned. I am not getting why it happens so?
#include<bits/stdc++.h>
using namespace std;
struct node{
int key;
};
void deletion(struct node* root){
root=NULL;
}
void print(struct node* temp){
if(!temp)
return;
cout<<temp->key;
}
int main(){
struct node* root = new struct node;
root->key=10;
cout<<"Initially : ";
print(root);
deletion(root);
cout<<"\nAfter deletion() : ";
print(root);
root=NULL;
cout<<"\nAfter assigning in main() : ";
print(root);
}
The output I am getting is :
Initially : 10
After deletion() : 10
After assigning in main() :
You're passing the pointer by value and modifying that value. To modify the value of a variable being passed, you can either pass it by reference (C++-style) or pass it by pointer(C-style). With C-style, remember to dereference the pointer to change its value for the caller.
C++-style:
void foo(const struct node*& n);
C-style:
void foo(const struct node** n);

Warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [duplicate]

This question already has answers here:
Warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11? [duplicate]
(2 answers)
Closed 3 years ago.
I can use this in main,insert and Display By Level as counter because i need the hight or the Tree
class BinarySearchTree
{
public:
Node* root;
int countHight=0; //in this line
BinarySearchTree()
{ root = NULL; }
~BinarySearchTree()
{ return; }
void insert(int value);
void display(Node* temp);
void DisplayByLevel(Node* temp,int level);
};
A C++ class definition is like a blueprint for something that doesn't exist yet, as such until you actually create an instance of the class, there is no variable to set to zero at initialisation. This is what the compiler is complaining about.
The only time that this would be valid is if the variable was declared static, but this would mean that every single instance of the class would effect the single static variable.
There are two solutions to this, as stated in the comments you could simply tell the compiler to use the C++11 standards which allow this method of initialisation, or you can use the more common and compatible method with older compilers which is to initialise it in the constructor (as you are already doing for root), like so:
class BinarySearchTree
{
public:
Node* root;
int countHight;
BinarySearchTree()
{
root = NULL;
countHight = 0;
}
~BinarySearchTree()
{
return;
}
void insert(int value);
void display(Node* temp);
void DisplayByLevel(Node* temp,int level);
};

expression must be a modifiable lvalue (for array) [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 5 years ago.
I have this program I'm working on, and one part of it is a linked list which I am working on making. I got this far, but at this point in the code it tells me (on the 3rd to last line) that inst must be a modifiable lvalue. I'm not sure what I'm doing wrong here.
#include <iostream>
using namespace std;
struct node{
float color[3];
float v[2*3];
node *next;
};
class TriangleList {
private: node *head;
private: node * tail;
public:
TriangleList() {
head = NULL;
tail = NULL;
}
void add(float vertices[], float colors[]) {
node *inst = new node;
inst->v = vertices;
}
};
Below statement is not correct, as you can't do inst->v = vertices because by doing this you are trying to change the base address of v, read the error properly.
error: incompatible types in assignment of ‘float’ to ‘float [6]’*
inst->v = vertices;
you may want to do like below
for(int i=0;i<len;i++) { /* len is the length of vertices */
inst->v[i] = vertices[i];
}

Object not instantiating properly

I am currently using VS2015 for this.
I am trying to create a binary search tree in c++ so that I can learn both the language and the data structure while trying to see if I can follow good practices. However, I am coming through a problem where I am not properly instantiating the object properly in the driver file.
BSTHeader.h
#pragma once
/*
Properties of Binary Search Tree:
1.) Elements less than root will go to the left child of root
2.) Elements greater than root will go to the right child of root
*/
#include <memory>
// Binary Search Tree handler class
class BSTHeader {
/*
Naive implementation of BSTNode (non-generic version)
Nested class is private, but it's internal fields and member functions
are public to outer class: BSTHeader
*/
class BSTNode {
public:
int data;
std::unique_ptr<BSTNode> left;
std::unique_ptr<BSTNode> right;
BSTNode(int val) {
data = val;
left = NULL;
right = NULL;
}
~BSTNode() {}
};
std::unique_ptr<BSTNode> root; // Root of BST
unsigned int size; // Total amount of nodes in tree from root
public:
BSTHeader();
BSTHeader(int val);
~BSTHeader();
bool insert(std::unique_ptr<BSTNode>& root, int val);
}
BSTHeader.cpp
#include "BSTHeader.h"
/*
Constructors:
*/
BSTHeader::BSTHeader() {
root = NULL;
size = 0;
}
BSTHeader::BSTHeader(int val) {
root = std::unique_ptr<BSTNode>(new BSTHeader::BSTNode(val)); // Smart pointer to an internal BSTNode
size = 1;
}
BSTHeader::~BSTHeader() {} // Empty destructor from use of smart pointer
/*
Member functions:
*/
bool BSTHeader::insert(std::unique_ptr<BSTNode>& root, int val) {
if (root == NULL) { // Place new element here
root = std::unique_ptr<BSTNode>(new BSTHeader::BSTNode(val));
size++;
return true;
}
if (val < root.get()->data) { // val < root
insert(root.get()->left, val);
}
else if (val > root.get()->data) { // val > root
insert(root.get()->right, val);
}
The issue I get is here, where I believe I am trying to instantiate a BSTHeader object.
Program.cpp
#include "BSTHeader.h"
int main()
{
BSTHeader::BSTHeader bst(); // <----- ERROR
return 0;
}
The error I am getting is cannot determine which instance of overloaded function "BSTHeader:BSTHeader" is intended
However, whenever I do:
BSTHeader bst()
I am not able to access the insert(..., ...) function for the object doing bst.insert(..., ...) due to expression must have class type even though the error above does not appear.
Yet everything works fine and I am able to access all the member methods by doing this: BSTHeader bst(5) by using the overloaded constructor.
I am not sure whether its a namespace issue or not. I feel as though I am missing something.
The line
BSTHeader::BSTHeader bst(); // <----- ERROR
is a declaration of a function named bst that takes no arguments and returns a BSTHeader::BSTHeader.
This is known as the "most vexing parse", and often described in less polite language.
If you want to instantiate an instance, giving the constructor no arguments, remove the ().

Changing where a function's behavior is described is causing an error?

I have the following class declaration (I've tried to remove as much excess code as possible):
class List {
public:
struct Node {
int value;
};
Node * findNode(unsigned int) {
return new Node;
}
};
This gives no error. However, as soon as I define the function "findNode" outside of the class, I get an error; here's the code:
class List {
public:
struct Node {
int value;
};
Node * findNode(unsigned int);
};
Node * List::findNode(unsigned int index) {
return new Node;
}
Now, when running the code, I get an error saying "LinkedList.cpp:9:1: error: 'Node' does not name a type".
I would appreciate any help in determining the problem.
Until the compiler encounters List:: it has no idea the Node you're talking about is a member of List. Change the definition to:
List::Node * List::findNode(unsigned int index) {
return new Node;
}
The "naked" Node inside the function is fine because by that time the compiler knows the function is a member of List.