简体   繁体   English

如何基于我自己的对象正确实现我的自定义节点类?

[英]How to implement my custom node class correctly based on my own object?

I have implemented a class called Customer, here is the header file: which worked perfectly. 我实现了一个名为Customer的类,这是头文件:效果很好。

Now, Based on my Customer class, I would like to implement a BST tree to store customer information, But I should need a node class which contains Customer object, pointers to left children and right children. 现在,基于我的客户类,我想实现一个BST树来存储客户信息,但是我需要一个节点类,其中包含客户对象,指向左孩子和右孩子的指针。 I have troubles about implement Node class correctly. 我有关于正确实现Node类的麻烦。 Especially for the Customer object. 特别是对于Customer对象。

class Customer {
public:
    Customer(string,char,int);
    string get_name();
    char get_initial();
    int get_account();
    void set_account(int);
    bool operator<(Customer & c);
    bool operator<=(Customer & c);
    bool operator>(Customer & c);
    bool operator>=(Customer & c);
    bool operator==(Customer & c);
    bool operator!=(Customer & c);

private:
    string name;
    char initial;
    int account;
    friend ostream& operator<<(ostream & os,  Customer & c);
};

#ifndef NODE_H_
#define NODE_H_

#include "Customer.h"

class Node {
public:
    Customer parent;
    Node* left;
    Node* right;
};

#endif /* NODE_H_ */

The error showed: is implicitly deleted because the default definition would be ill-formed: 显示的错误:被隐式删除,因为默认定义格式不正确:

The problem here is about the default constructor of the Node class. 这里的问题与Node类的default constructor有关。 The default constructor, is automatically generated in class , if there is no other user-defined constructors. 如果没有其他用户定义的构造函数,则会在class自动生成默认的构造函数。 So, the default constructor tries to generate the default values to all of the class's members, and run into problems when it tries to call the default constructor of the object Customer which is no exists (because the constructor Customer(string,char,int); exists, and there is no user-defined default constructor for the Customer class). 因此,默认构造函数尝试为类的所有成员生成默认值,并在尝试调用不存在的对象Customer的默认构造函数时遇到问题(因为构造函数Customer(string,char,int);存在,并且Customer类没有用户定义的默认构造函数。 There are several solutions you can use (Sorted from most recommended to least recommended, in this particular case. in other cases this rate might have some changes): 您可以使用多种解决方案(在这种情况下,从最推荐到最不推荐。在其他情况下,此比率可能会有一些变化):


Solution 1 (Recommended) 解决方案1(推荐)

Define a constructor in the Node class, that use the non-default constructor of the Customer class member parent : Node类中定义一个构造函数,该构造函数使用Customer类成员parent的非默认构造函数:

 class Node { public: Node(string name, char c, int age) : parent(name, c, age) {} Customer parent; Node* left; Node* right; }; 

Solution 2 解决方案2

Define default constructor for Customer class: Customer类定义默认构造函数:

 class Customer { public: /*...*/ Customer(); /*...*/ } 

Why doesn't it the most recommended way in this case? 为什么在这种情况下不是最推荐的方法呢? It solves the compiler's error with less code than the most recommended way, so why not choosing this way? 它比最推荐的方法用更少的代码解决了编译器的错误,那么为什么不选择这种方法呢? This way have logical issue- What does an empty customer mean? 这种方式存在逻辑问题-空客户是什么意思? How do you know when a Customer object is empty? 您如何知道Customer对象为空? You can see my answer here: C++ vector of struct allocated on stack to get an idea of identify an object that have been created without any user-defined data. 您可以在这里看到我的答案: 在堆栈分配的struct结构的C ++向量,使您可以识别没有任何用户定义的数据而创建的对象。 Those validations are a little bit more complicated than forcing the user to set some data ahead. 这些验证比强迫用户提前设置一些数据要复杂得多。

However - this way might be the best way when there are no logical issues like the one I mentioned before, or whenever there is no need to deal with validations, or in cases where the validations are worth the choosing in this solution. 但是,当没有像我之前提到的逻辑问题时,或者不需要处理验证时,或者在这种解决方案中值得选择验证的情况下,这种方法可能是最好的方法。


Solution 3 (Highly not recommended) 解决方案3(不推荐)

Define parent in Node class as pointer (so the default value will be nullptr ). Node类中将parent定义为指针(因此默认值为nullptr )。

Why not choosing this option? 为什么不选择此选项? This option might be the easiest and fastest way to make the compiler error disappear- However it will force you to handle pointers of pointers and memory management all the way in your program, and will make your life much harder than the other solutions (after the compiler error gone - segfault ( What is a segmentation fault? ) are much more annoying than compiler errors). 此选项可能是使编译器错误消失的最简单,最快的方法-但是,它将迫使您在程序中始终处理指针的指针和内存管理,并且将使您的生活比其他解决方案困难得多(编译器错误消失了segfault什么是分段错误? )比编译器错误更令人讨厌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中正确实现我自己的异常处理程序 - How to correctly implement my own exception handler in C++ 如何做我自己的自定义运行时错误类? - How to do my own custom runtime error class? 为什么我的MinHeap正确排序整数而不是我自己的类 - Why is my MinHeap sorting integers correctly but not my own class 尝试实现我自己的 &lt;&lt; 和 &gt;&gt; 流操作符来在我的网格 object 上操作 - Trying to implement my own << and >> streaming operators to operate on my Grid object 实现我自己的无私 - implement my own myless 如何为我的自定义C ++类实现副本构造函数 - How do Implement the copy constructor for my custom c++ class 如何在C ++中自己的类中正确使用矩阵(本征库)? - How to correctly use a matrix (Eigen library) in my own class in C++? 我在 SFML 中的“身体”class:它如何自行正确旋转成员形状? - My 'body' class in SFML: How does it rotate member shapes correctly on its own? 如何创建我自己的“cout”和“cerr”类 - how to create my own 'cout' and 'cerr' class 如何修改此基于MFC的代码段以使用我自己选择的窗口类名? - How can I modify this MFC-based code snippet to use a window class name of my own choice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM