Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 动态分配自定义类的数组和重载运算符_C++_Arrays_Operator Overloading_Dynamic Memory Allocation - Fatal编程技术网

C++ 动态分配自定义类的数组和重载运算符

C++ 动态分配自定义类的数组和重载运算符,c++,arrays,operator-overloading,dynamic-memory-allocation,C++,Arrays,Operator Overloading,Dynamic Memory Allocation,在过去的几天里,我一直在试图弄清楚如何为我创建的这个类创建一个动态分配的数组。其中一个包括我制作的另一个类 以下是课程: template<typename T> class BST // a binary search tree class { private: Node<T> * root; int size; public: BST() { root = NULL; size = 0; } /

在过去的几天里,我一直在试图弄清楚如何为我创建的这个类创建一个动态分配的数组。其中一个包括我制作的另一个类

以下是课程:

template<typename T>
class BST // a binary search tree class
{
private:
    Node<T> * root;
    int size;
public:
    BST()
    {
        root = NULL;
        size = 0;
    }

/*void * BST::operator new[](size_t a) //tried it both with the "BST::" and without
    {
        void * p = malloc(a);//24
        return p;
    }*/
//there are more functions that i cut out
}
问题是它在运行时只生成一个BST对象。我做了一些研究,并尝试重载新的[]操作符,但它完全没有任何作用


有人能解释一下这样做的正确方法吗?

数组中确实有多个对象,但t不是数组

t是指向一个BST的指针,调试器会将其显示为这样的值–调试器不知道它是指向数组第一个元素的指针

如果要将其视为数组,则需要通知调试器执行此操作。
您可以在“监视”窗口中执行此操作,我认为显示前两个元素的语法应该是t,2。

是要在开始时分配所有节点,还是要在向树中添加新项时动态增长树?此外,您应该在节点的构造函数中使用初始值设定项语法,因为在当前的设计中,您将T限制为可以分配给NULL的类型。问题是,它在运行时只生成一个BST对象,而生成n个对象。你是说n==1吗?@juanchopanza我将n设为3,它只会生成一个对象。@ChrisDouglas不太可能,除非代码在一个对象后崩溃。或者它没有发生。@Rudi当我第一次创建一个BST对象时,根节点应该为空,然后可以插入对象。不过,分配节点对象不会带来任何问题。就在我尝试创建BST对象数组时:
template <typename T>
class Node //a node for a binary search tree
{
public:
    int key;
    T data;
    Node * left;
    Node * right;
    Node * parent;
//public:
    Node<T>()
    {
        key = NULL;
        data = NULL;
        left = NULL;
        right = NULL;
        parent = NULL;
    }
//more stuff below this but it's just getting and setting the data
}
BST<char> * t = new BST<char>[ n ]; //the user will give the value for int n