0

I am from Java background. My java class:

class Node {
    public Node left;
    public Node right;
    public int deep;
    public Point p;  //another class
}

When I tried to convert it into C++, I faced error: Field has incomplete type Node. So, based on some online help, I converted it to following:

class Node {
public:
    Node* left;
    Node* right;
    int deep;
    Point p;  //another class
}

But my other part of code breaks now. The java code was:

Node pathNode = new Node();
if (pathNode.left == null) {
    pathNode = pathNode.left;
}

I really want to know how to implement it in C++. My try so far in C++:

class Node {
public:
    Node* left;
    Node* right;
    int deep;
    Point p;
    Node() {
         this->left = nullptr;
         this->right = nullptr;
         this->deep = NULL;   // not sure correct or wrong
         this->p = NULL      //  not sure correct or wrong
    }

then other part of c++ code:

Node pathNode;
if (pathNode.left == nullptr) {
    pathNode = pathNode.left;   //<== here i am stuck exactly.
}

OR if there is any better way, you can suggest me. Moreover, how to set class members to NULL or nullptr?

foobar
  • 571
  • 1
  • 5
  • 20
  • 2
    "`pathNode = pathNode.left;`" well, `pathNode` is a `Node`, but `pathNode.left` is a `Node*`. So, perhaps use `Node* pathNode = nullptr;`? – Andy Turner Jul 07 '20 at 09:38
  • 1
    There is a list of good C++ books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The similarities between Java and C++ are mostly syntactical. In particular, the fundamental things are fundamentally different. – molbdnilo Jul 07 '20 at 10:52

1 Answers1

1

If I understood you correctly, you could write your Node class like so in C++.

class Node {
public:
    Node* left;
    Node* right;
    int deep;
    //Point p;
    
    Node() :
        left(nullptr),
        right(nullptr),
        deep(0)
        /*p()*/ {
    }
};

int main() {
    Node* pathNode = new Node();
    
    if (pathNode->left == nullptr) {
        pathNode = pathNode->left;  
    }
    
    if (pathNode == nullptr) { // check if indeed nullptr
        std::cout << "nullptr"<< std::endl;
    }
    
    return 0;
}

EDIT: int can't be NULL or nullptr because all values in an integer are valid.

Drejc
  • 491
  • 6
  • 21
  • So, how to declare `null` the Point class? do I just need to un-comment the code of `p` and `Point` that you commented? – foobar Jul 07 '20 at 10:01
  • Moreover, the `Node():` style of declaring constructor is new to me. It gives me syntax errors. Are you sure there are no `{}` in constructor. And, `left(nullptr), (nullptr),` have no semicolons?? – foobar Jul 07 '20 at 10:05
  • 1
    Basically you can't because the `Point p` is not declared as a pointer. What you should do to declare it first as `Point* p`, then after `deep(0)` put a comma, and right after insert `p(nullptr)`. – Drejc Jul 07 '20 at 10:06
  • 1
    Yes I'm sure, this convention is used in c++ to initialize data members inside the class (like `left` and `right`). And there still are `{ }` in the constructor. [link](https://en.cppreference.com/w/cpp/language/constructor) – Drejc Jul 07 '20 at 10:10