Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.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++ 调试断言失败!表达式:块类型是否有效(标题->;\u块使用)_C++ - Fatal编程技术网

C++ 调试断言失败!表达式:块类型是否有效(标题->;\u块使用)

C++ 调试断言失败!表达式:块类型是否有效(标题->;\u块使用),c++,C++,程序正确运行并输出所有内容,并抛出此错误。对于这意味着什么,以及如何找到或修复它,我还没有找到任何好的解释。下面是代码的完整副本(非常难看而且写得很糟糕): #包括 使用名称空间std; /*用于存储二叉树的类*/ 模板 类二叉树{ 受保护的: 类型parentArray[10]; 类型childArray[10]; 公众: 二叉树(); 二叉树(int&k); ~BinaryTree(); BinaryTree(BinaryTree&bt); void运算符=(二进制树&bt); friend

程序正确运行并输出所有内容,并抛出此错误。对于这意味着什么,以及如何找到或修复它,我还没有找到任何好的解释。下面是代码的完整副本(非常难看而且写得很糟糕):

#包括
使用名称空间std;
/*用于存储二叉树的类*/
模板
类二叉树{
受保护的:
类型parentArray[10];
类型childArray[10];
公众:
二叉树();
二叉树(int&k);
~BinaryTree();
BinaryTree(BinaryTree&bt);
void运算符=(二进制树&bt);

friend ostream&operator您的
二进制树
析构函数始终确保:

#include <iostream>
using namespace std;

/* a class for storing a Binary Tree */
template <class Type>
class BinaryTree {
protected:
    Type parentArray[10];
    Type childArray[10];
public:
    BinaryTree();
    BinaryTree(int& k);
    ~BinaryTree();
    BinaryTree(BinaryTree<Type>& bt);
    void operator= (BinaryTree<Type>& bt);
    friend ostream& operator<< (ostream& s, BinaryTree<Type> bt) {
        s << "[ ";
        bt.inorder(bt.getRoot());
        s << "]" << endl;
        return s;
    };
    int size();
    int height();
    int getLeft(int k);
    int getRight(int k);
    void preorder(int k);
    void inorder(int k) {

        // do I have a left child?
        if ((getLeft(k)) != -1) {
            // if yes inorder (left child)
            inorder(getLeft(k));
        };
        // output k
        cout << k << " ";
        // do I have a right child?
        if ((getRight(k)) != -1) {
            // if yes inorder (right child)
            inorder(getRight(k));
        };
    };
    void postorder(int k);
    void setRoot(Type& val);
    void setParent(Type* child, Type* parent);
    void setLeft(Type& val);
    void setRight(Type& val);
    int getRoot();
};

/* default constructor */
template <class Type>
BinaryTree<Type>::BinaryTree() {
    parentArray = new ArrayClass<Type>();
    childArray = new ArrayClass<Type>();
};

/* non-empty constructor */
template <class Type>
BinaryTree<Type>::BinaryTree(int& k) {
//  parentArray = new Type[k];
//  childArray = new Type[k];
};

template <class Type>
BinaryTree<Type>::~BinaryTree() {
    delete[] parentArray;
    delete[] childArray;
};

template <class Type>
BinaryTree<Type>::BinaryTree(BinaryTree<Type>& bt) {
    for (int i = 0; i < bt.size(); i++) {
        parentArray[i] = bt.parentArray[i];
        childArray[i] = bt.childArray[i];
    };
};

template <class Type>
void BinaryTree<Type>::operator= (BinaryTree<Type>& bt) {

};


/* return the size of the tree using the length of the parent array */
template <class Type>
int BinaryTree<Type>::size() {

    return (sizeof(parentArray)/sizeof(*parentArray));
};


template <class Type>
int BinaryTree<Type>::height() {
    return 5;
};

template <class Type>
int BinaryTree<Type>::getLeft(int k) {

    // if the parent array value of the given number is k and 
    // the child array value indicates it is a left child
    for (int i = 0; i < size(); i++) {
        if ((parentArray[i] == k) && (childArray[i] == 0)) {

            // return that value
            return i;
        };
    };
    return -1;
};

template <class Type>
int BinaryTree<Type>::getRight(int k) {

    // if the parent array value of the given number is k and 
    // the child array value indicates it is a right child
    for (int i = 0; i < size(); i++) {
        if ((parentArray[i] == k) && (childArray[i] == 1)) {

            // return that value
            return i;
        };
    };
    return -1;
};

template <class Type>
void BinaryTree<Type>::preorder(int k) {
    // output k
    cout << k << " ";
    // do I have a left child?
    if ((getLeft(k)) != -1) {
        // if yes preorder left child
        preorder(getLeft(k));
    };
    // do I have a right child?
    if ((getRight(k)) != -1) {
        // if yes preorder right child
        preorder(getRight(k));
    };
};

template <class Type>
void BinaryTree<Type>::postorder(int k) {
    // do I have a left child?
    if ((getLeft(k)) != -1) {
        // if yes inorder (left child)
        inorder(getLeft(k));
    };
    // do I have a right child?
    if ((getRight(k)) != -1) {
        // if yes inorder (right child)
        inorder(getRight(k));
    };
    // output k
    cout << k << " ";
};

template <class Type>
void BinaryTree<Type>::setRoot(Type& val) {
    // if the given value is the root of the tree then set
    // its index in the parent and child arrays to -1
    parentArray[val] = -1;
    childArray[val] = -1;
};

template <class Type>
void BinaryTree<Type>::setParent(Type* child, Type* parent) {

    // set a given value as the parent of a given value
    parentArray[(*child)] = *parent;
};

template <class Type>
void BinaryTree<Type>::setLeft(Type& val) {

    // set a given value in the child array to indicate a left child
   childArray[val] = 0;
};

template <class Type>
void BinaryTree<Type>::setRight(Type& val) {

    // set a given value in the child array to indicate a right child
    childArray[val] = 1;
};

template <class Type>
int BinaryTree<Type>::getRoot() {

    // find the root value of the tree
    for (int i = 0; i < size(); i++) {
        if (parentArray[i] == -1) {

            // and return it
            return i;
        };
    };
};

int  main() {
    int* val1 = new int;
    int* val2 = new int;
    int* val3 = new int;
    int count;

    cin >> count;

    BinaryTree<int> bt(count);

    for (int i = 0; i < count; i++) {
        cin >> *val1;
        cin >> *val2;
        cin >> *val3;

        if (i == 0) {
            bt.setRoot(*val1);
        };

        if (*val2 != -1) {
            bt.setParent(val2, val1);
            bt.setLeft(*val2);
        }
        if (*val3 != -1) {
            bt.setParent(val3, val1);
            bt.setRight(*val3);
        }

        val1 = new int;
        val2 = new int;
        val3 = new int;
    };

    cout << bt.size() << endl;
    bt.postorder(bt.getRoot());
    cout << endl;
    bt.preorder(bt.getRoot());
    cout << endl;

    delete val1;
    delete val2;
    delete val3;
};
不幸的是,该类的一个构造函数没有
new
这些数组中的任何一个。因此,析构函数最终尝试
删除一对未初始化的垃圾指针

也有可能是这个类,但我还没有充分分析


编辑:正如在评论中指出的,这些不是指针;因此这是错误的,但出于其他原因。

这些数组不是指针。不需要
新建它们。它们不应该被删除。
中的
for
循环正在泄漏内存。
val1
val2
val3是循环底部的新代码,但从未释放。建议:在测试之前更频繁地测试或编写较小的代码块。当测试一个函数的新代码时,通常很容易找出错误所在。编写构造函数和析构函数并进行测试。添加一个方法。测试。添加另一个方法并进行测试。冲洗。重复.这回答了你的问题吗?
#include <iostream>
using namespace std;

/* a class for storing a Binary Tree */
template <class Type>
class BinaryTree {
protected:
    Type parentArray[10];
    Type childArray[10];
public:
    BinaryTree();
    BinaryTree(int& k);
    ~BinaryTree();
    BinaryTree(BinaryTree<Type>& bt);
    void operator= (BinaryTree<Type>& bt);
    friend ostream& operator<< (ostream& s, BinaryTree<Type> bt) {
        s << "[ ";
        bt.inorder(bt.getRoot());
        s << "]" << endl;
        return s;
    };
    int size();
    int height();
    int getLeft(int k);
    int getRight(int k);
    void preorder(int k);
    void inorder(int k) {

        // do I have a left child?
        if ((getLeft(k)) != -1) {
            // if yes inorder (left child)
            inorder(getLeft(k));
        };
        // output k
        cout << k << " ";
        // do I have a right child?
        if ((getRight(k)) != -1) {
            // if yes inorder (right child)
            inorder(getRight(k));
        };
    };
    void postorder(int k);
    void setRoot(Type& val);
    void setParent(Type* child, Type* parent);
    void setLeft(Type& val);
    void setRight(Type& val);
    int getRoot();
};

/* default constructor */
template <class Type>
BinaryTree<Type>::BinaryTree() {
    parentArray = new ArrayClass<Type>();
    childArray = new ArrayClass<Type>();
};

/* non-empty constructor */
template <class Type>
BinaryTree<Type>::BinaryTree(int& k) {
//  parentArray = new Type[k];
//  childArray = new Type[k];
};

template <class Type>
BinaryTree<Type>::~BinaryTree() {
    delete[] parentArray;
    delete[] childArray;
};

template <class Type>
BinaryTree<Type>::BinaryTree(BinaryTree<Type>& bt) {
    for (int i = 0; i < bt.size(); i++) {
        parentArray[i] = bt.parentArray[i];
        childArray[i] = bt.childArray[i];
    };
};

template <class Type>
void BinaryTree<Type>::operator= (BinaryTree<Type>& bt) {

};


/* return the size of the tree using the length of the parent array */
template <class Type>
int BinaryTree<Type>::size() {

    return (sizeof(parentArray)/sizeof(*parentArray));
};


template <class Type>
int BinaryTree<Type>::height() {
    return 5;
};

template <class Type>
int BinaryTree<Type>::getLeft(int k) {

    // if the parent array value of the given number is k and 
    // the child array value indicates it is a left child
    for (int i = 0; i < size(); i++) {
        if ((parentArray[i] == k) && (childArray[i] == 0)) {

            // return that value
            return i;
        };
    };
    return -1;
};

template <class Type>
int BinaryTree<Type>::getRight(int k) {

    // if the parent array value of the given number is k and 
    // the child array value indicates it is a right child
    for (int i = 0; i < size(); i++) {
        if ((parentArray[i] == k) && (childArray[i] == 1)) {

            // return that value
            return i;
        };
    };
    return -1;
};

template <class Type>
void BinaryTree<Type>::preorder(int k) {
    // output k
    cout << k << " ";
    // do I have a left child?
    if ((getLeft(k)) != -1) {
        // if yes preorder left child
        preorder(getLeft(k));
    };
    // do I have a right child?
    if ((getRight(k)) != -1) {
        // if yes preorder right child
        preorder(getRight(k));
    };
};

template <class Type>
void BinaryTree<Type>::postorder(int k) {
    // do I have a left child?
    if ((getLeft(k)) != -1) {
        // if yes inorder (left child)
        inorder(getLeft(k));
    };
    // do I have a right child?
    if ((getRight(k)) != -1) {
        // if yes inorder (right child)
        inorder(getRight(k));
    };
    // output k
    cout << k << " ";
};

template <class Type>
void BinaryTree<Type>::setRoot(Type& val) {
    // if the given value is the root of the tree then set
    // its index in the parent and child arrays to -1
    parentArray[val] = -1;
    childArray[val] = -1;
};

template <class Type>
void BinaryTree<Type>::setParent(Type* child, Type* parent) {

    // set a given value as the parent of a given value
    parentArray[(*child)] = *parent;
};

template <class Type>
void BinaryTree<Type>::setLeft(Type& val) {

    // set a given value in the child array to indicate a left child
   childArray[val] = 0;
};

template <class Type>
void BinaryTree<Type>::setRight(Type& val) {

    // set a given value in the child array to indicate a right child
    childArray[val] = 1;
};

template <class Type>
int BinaryTree<Type>::getRoot() {

    // find the root value of the tree
    for (int i = 0; i < size(); i++) {
        if (parentArray[i] == -1) {

            // and return it
            return i;
        };
    };
};

int  main() {
    int* val1 = new int;
    int* val2 = new int;
    int* val3 = new int;
    int count;

    cin >> count;

    BinaryTree<int> bt(count);

    for (int i = 0; i < count; i++) {
        cin >> *val1;
        cin >> *val2;
        cin >> *val3;

        if (i == 0) {
            bt.setRoot(*val1);
        };

        if (*val2 != -1) {
            bt.setParent(val2, val1);
            bt.setLeft(*val2);
        }
        if (*val3 != -1) {
            bt.setParent(val3, val1);
            bt.setRight(*val3);
        }

        val1 = new int;
        val2 = new int;
        val3 = new int;
    };

    cout << bt.size() << endl;
    bt.postorder(bt.getRoot());
    cout << endl;
    bt.preorder(bt.getRoot());
    cout << endl;

    delete val1;
    delete val2;
    delete val3;
};
delete[] parentArray;
delete[] childArray;