简体   繁体   English

Class 实例成员依赖于 class 成员属性

[英]Class instance member that depends on class member attribute

My faculty provided me with a class that represents a Binary Tree and we have to use it for an assigment.我的教师为我提供了一个代表二叉树的 class,我们必须将其用于分配。 So I'm doing a class called cluster and the thing goes like this:所以我正在做一个名为 cluster 的 class ,事情是这样的:

BinTree (I've only copied what I though it's the minimum necessary): BinTree (我只复制了我所需要的最低限度的内容):

template <typename T>
class BinTree {
public:

// Constructs an empty tree. Θ(1).
BinTree ()
:   p(nullptr)
{   }

// Constructs a tree with a value x and no subtrees. Θ(1).
explicit BinTree (const T& x);

// Constructs a tree with a value x and two subtrees left and right. Θ(1).
explicit BinTree (const T& x, const BinTree& left, const BinTree& right);

Cluster:簇:

class Cluster {
    private:

    BinTree<std::pair<std::string, double>> _cluster;

    public:
    Cluster();
    //etc....
}

Since I can't use inheritance (We haven't reached that part yet) I really don't have any idea how the constructors of Cluster would be.由于我不能使用 inheritance (我们还没有达到那个部分)我真的不知道 Cluster 的构造函数是怎样的。 A cluster object would be a Binary tree but I start with the "leaves"(English isn't my first language so I don't know how to call it), therefore I have to create a Cluster with a string an a double = 0.0.集群 object 将是一棵二叉树,但我从“叶子”开始(英语不是我的第一语言,所以我不知道如何称呼它),因此我必须创建一个带有字符串的集群 a double = 0.0。

I've assumed the Cluster constructor would be like this:我假设 Cluster 构造函数是这样的:

Cluster(const std::string& id) : _cluster(make_pair(id, 0.0)){};

Is this correct?这个对吗?

Then, having 2 specific Clusters I will merge them into a one.然后,拥有 2 个特定的集群,我会将它们合并为一个。 This new Cluster, because its _cluster attribute is a binary tree, would be the parent of the previous ones, here is were I would have to use the 3rd constructor of BinTree but I don't know how to do it.这个新的集群,因为它的 _cluster 属性是一个二叉树,将是以前的父级,这是我必须使用 BinTree 的第三个构造函数,但我不知道该怎么做。

From what I understood, you want to merge 2 clusters so, you can do it similary to how it is done in the BinTree class.据我了解,您想合并 2 个集群,因此您可以像在 BinTree class 中那样做。

explicit Cluster ( Cluster& left, const Cluster& right){
    _cluster = Cluster(left, right) ;
}

I don't undeestand the functionality of the cluster class, it might be easier to use BinTree directly.我不理解集群 class 的功能,直接使用 BinTree 可能更容易。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM