将指针类分配给新类,C++
Assigning pointer classes to new Class, C++
我来自Java背景。我的 java 类:
class Node {
public Node left;
public Node right;
public int deep;
public Point p; //another class
}
当我尝试将其转换为C++时,我遇到了错误:Field has incomplete type Node
。因此,根据一些在线帮助,我将其转换为以下内容:
class Node {
public:
Node* left;
Node* right;
int deep;
Point p; //another class
}
但是我的另一部分代码现在坏了。java代码是:
Node pathNode = new Node();
if (pathNode.left == null) {
pathNode = pathNode.left;
}
我真的很想知道如何在C++中实现它。到目前为止,我在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
}
然后是 C++ 代码的其他部分:
Node pathNode;
if (pathNode.left == nullptr) {
pathNode = pathNode.left; //<== here i am stuck exactly.
}
或者如果有更好的方法,你可以建议我。此外,如何将类成员设置为 NULL 或 nullptr?
如果我理解正确,你可以这样写你的Node
类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;
}
编辑:int不能NULL
或nullptr
,因为整数中的所有值都是有效的。
相关文章:
- 将指针类分配给新类,C++
- VS2015/VC++ 在新类模板中禁用默认 #include "stdafx.h"
- 实例化新类时未调用的方法
- C++用一个新类初始化二维向量;默认值更改
- 来自抽象类的新类
- 在Visual Studio代码中为C 创建一个新类
- 我的二传手在新类中不起作用(继承)
- 如何创建一个新类来继承 ostream 并将其用作 cout 但带有锁定
- (C++)我需要在新类的头文件中包含什么
- 如何防止类被错误化,只允许新类?
- 关于包括新类的基本问题
- 具有不同地图的新类中的C 重复行为
- 如何在Google V8中添加一个新类
- 将 QString 传递给类构造函数会更改新类中的 QString
- 如何创建新类以部署opencv和对象的方法
- C++ 带参数的新类数组
- 避免每次要引入新功能时都使用新类
- 新的新类对象的函数;错误:非静态成员引用必须相对于特定对象
- 什么是(新类())->函数();意味 着?
- 使用工厂朋友类分配新类,好习惯