Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++的新手,最近我收到了一个使用模板创建自己的二进制搜索树的项目。目标是使二叉树能够接受任何类型的数据。 我创建了两个类:节点(h,cpp)、树(h,cpp)和主cpp。 由于某种原因,我总是被侵犯 (很可能是由于析构函数的原因,它得到了一个损坏的值)_C++_Templates_Tree_Runtime Error - Fatal编程技术网

访问冲突二进制搜索树模板 我是C++的新手,最近我收到了一个使用模板创建自己的二进制搜索树的项目。目标是使二叉树能够接受任何类型的数据。 我创建了两个类:节点(h,cpp)、树(h,cpp)和主cpp。 由于某种原因,我总是被侵犯 (很可能是由于析构函数的原因,它得到了一个损坏的值)

访问冲突二进制搜索树模板 我是C++的新手,最近我收到了一个使用模板创建自己的二进制搜索树的项目。目标是使二叉树能够接受任何类型的数据。 我创建了两个类:节点(h,cpp)、树(h,cpp)和主cpp。 由于某种原因,我总是被侵犯 (很可能是由于析构函数的原因,它得到了一个损坏的值),c++,templates,tree,runtime-error,C++,Templates,Tree,Runtime Error,。。。这是我的代码(这是整个过程,所以它会更容易…) . 谢谢 template <typename T> std::ostream& operator<<(std::ostream& output, const Tree<T>& tree) { if (!tree.getRoot()) return output; Node<T> * temp=tree.getRoot(); Tr

。。。这是我的代码(这是整个过程,所以它会更容易…) . 谢谢

template <typename T>
std::ostream& operator<<(std::ostream& output, const Tree<T>& tree)
{
    if (!tree.getRoot())
        return output;
    Node<T> * temp=tree.getRoot();

    Tree<T>  tempL(temp->getL());
    output<<tempL;

    output<<temp->getValue();
    output<<endl;


    Tree<T> tempR(temp->getR());
    output<<tempR;


    return output;
}

#endif

#include "Tree.h"

void main ()
{
    Node<int> *no=new Node<int>(7);
    Tree<int> IntTree(no);
    IntTree.insert(new Node<int> (5));
    IntTree.insert(new Node<int> (0));
    IntTree.insert(new Node<int> (4));
    IntTree.insert(new Node<int> (5));
    IntTree.insert(new Node<int> (12));
    IntTree.insert(new Node<int> (7));
    IntTree.insert(new Node<int> (1));
    IntTree.insert(new Node<int> (14));
    IntTree.insert(new Node<int> (7));
    IntTree.insert(new Node<int> (51));
        if(IntTree.exists(5.1))
        cout<<IntTree;

}
模板

std::ostream&operator问题似乎是节点被删除了两次。在
operatorHint内部:让树使用固定的数据类型,例如
int
,然后转换为模板。调试容易得多。使用调试器时,哪个语句会导致问题?removeTree(node->getL());让调试器停止@Thomasmatthew我以前没有这样做过,但它确实起了作用……当我们要求时,我们不是要求您执行
Ctrl+A
,然后执行
Ctrl+C
。此外,在输出操作符中,您可以创建树的副本。这将创建根指针的副本,然后在析构函数中删除它们。这将多次删除同一指针值。通常,若在对象中存储原始指针,则还必须具有复制构造函数和赋值运算符,以创建指向的对象的深度副本。看见
template <typename T>
std::ostream& operator<<(std::ostream& output, const Tree<T>& tree)
{
    if (!tree.getRoot())
        return output;
    Node<T> * temp=tree.getRoot();  // temp->getRoot() wil be deleted temp is destroyed
                                    // this will also destroy temp->getL() and temp->getR()
                                    // because of Node<T>'s destructor

    Tree<T>  tempL(temp->getL());   // will delete temp->getL() when tempL is destroyed
    output<<tempL;

    output<<temp->getValue();
    output<<endl;


    Tree<T> tempR(temp->getR());    // will delete temp->getL() when tempR is destroyed
    output<<tempR;


    // tempL and tempR are destroyed
    return output;
}