程序运行不断失败
Program run keeps failing
以下代码的运行失败。
这是我的代码:
#ifndef STACK_H
#define STACK_H
//#include "BinaryTree.h"
using namespace std;
template<class T>
class stack
{
public:
stack(); // constructor
T pop(); // pop with type BinaryTree
void push(T x); // push BinaryTree on top
bool empty(); // return t/f if stack is empty
int size(); // return size to keep track of stack
private:
T arr[10]; // array with 10 elements
int ele; // keeps track of top of list
};
/******************************************************/
template<class T>
stack<T>::stack()
{
ele = 0;
}
template<class T>
T stack<T>::pop()
{
return arr[--ele];
}
template<class T>
void stack<T>::push(T x)
{
arr[ele++] = x;
}
template<class T>
bool stack<T>::empty()
{
if(ele == 0)
{
return true;
}
}
template<class T>
int stack<T>::size()
{
return ele;
}
#endif /* STACK_H */
#ifndef BINARYTREE_H
#define BINARYTREE_H
using namespace std;
我需要 3 个构造函数;对于第三个构造函数,它不会处理。我认为这是因为我正在调用来自同一类的另一个构造函数。
template<typename T> class BinaryTree
{
public:
// Binary Tree Things
BinaryTree(); // default constructor to make empty tree
BinaryTree(T ro); // default constructor 2 to make tree with only root
BinaryTree(T ro, T le, T ri); // default constructor 3 to make complete binary tree
//~BinaryTree(); // destructor for dynamics
bool isEmpty(); // method that returns t/f if tree is empty
T info(); // method to return value in root of the tree
void inOrder(); // traverses nodes in a tree left, root, right
void preOrder(); // traverses nodes in a tree root, left, right
void postOrder(); // traverses nodes in a tree left, right, root
private:
struct Tree_Node // represents a node
{
T Node_Info;
BinaryTree<T> *left; // left pointer
BinaryTree<T> *right; // right pointer
};
Tree_Node *root; // create root with 2 pointers from this };
};
/***********************************************************************/
template<typename T> BinaryTree<T>::BinaryTree()
{
}
template<typename T> BinaryTree<T>::BinaryTree(T ro)
{
this->root->Node_Info = ro;
this->root->left = 0;
this->root->right = 0;
}
template<typename T> BinaryTree<T>::BinaryTree(T ro, T le, T ri)
{
// create temps for left and right
BinaryTree<T> *templeft = new BinaryTree(le);
templeft->root->Node_Info = le;
BinaryTree<T> *tempright = new BinaryTree(ri);
tempright->root->Node_Info = ri;
// re-assign everything
this->root->Node_Info = ro;
this->root->left = templeft;
this->root->right = tempright;
}
/*template<typename T> BinaryTree<T>::~BinaryTree() {
delete root; }*/
template<typename T> bool BinaryTree<T>::isEmpty()
{
return false;
}
template<typename T> T BinaryTree<T>::info()
{
}
template<typename T> void BinaryTree<T>::inOrder()
{
}
template<typename T> void BinaryTree<T>::preOrder()
{
}
template<typename T> void BinaryTree<T>::postOrder()
{
}
#endif /* BINARYTREE_H */
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <ctime>
#include <limits>
//#include "BinaryTree.h"
//#include "stack.h"
using namespace std;
int main()
{
stack<BinaryTree<char> > testing;
BinaryTree<char> testing2('d', 'd', 'd');
testing.push(testing2);
cout << testing.size();
return 0;
}
您正在按值推送二叉树:
stack<BinaryTree<char> > testing;
BinaryTree<char> testing2('d', 'd', 'd');
testing.push(testing2);
然而,BinaryTree 不支持复制,因为它会做一个浅拷贝(没有三法则特殊成员(。这意味着,副本将共享root
指针,并且两个 BinaryTree 将delete
相同的root
(假设您取消注释该关键代码(。
这是一个修复程序,可将必要的特殊成员添加到BinaryTree<T>
和BinaryTree<T>::Tree_Node
:
(复制(构造函数 +
BinaryTree<T>
析构函数BinaryTree(BinaryTree const& other) : root(other.root? new Tree_Node(*other.root) : 0) {}
(复制(构造函数 +
BinaryTree<T>::Tree_Node
析构函数struct Tree_Node // represents a node { T data; Tree_Node *left; // left pointer Tree_Node *right; // right pointer Tree_Node(T data, Tree_Node* left = 0, Tree_Node* right = 0) : data(data), left(left), right(right) {} Tree_Node(Tree_Node const& other) : data(other.data), left (other.left? new Tree_Node(*other.left) : 0), right(other.right?new Tree_Node(*other.right) : 0) {} ~Tree_Node() { delete left; delete right; } };
- 请注意,我更改了
Tree_Node
,因此它拥有其他Tree_Node
而不是完整的BinaryTree(此更改是相当无端的,源于我在尝试修复任何内容之前尝试减少噪音(
- 请注意,我更改了
同样在"降噪"类别中,我已经重新
stack<T>
std::vector<T>
之上,只是为了排除这是错误来源。
大免责声明:现在,这些代码中没有多少实际上是异常安全的。我假设异常安全尚未出现在本课程的菜单上。编辑但查看评论。
在 IdeOne 上直播:
#ifndef STACK_H
#define STACK_H
//#include "BinaryTree.h"
using namespace std;
#include <cassert>
#include <vector>
template<class T>
class stack
{
public:
T pop() { assert(!empty()); T v = _data.back(); _data.pop_back(); return v; }
void push(T x) { _data.push_back(x); }
bool empty() { return _data.empty(); }
int size() { return _data.size(); }
private:
std::vector<T> _data;
};
#endif /* STACK_H */
#ifndef BINARYTREE_H
#define BINARYTREE_H
using namespace std;
template<typename T> class BinaryTree
{
public:
// Binary Tree Things
BinaryTree(); // default constructor to make empty tree
BinaryTree(T ro); // default constructor 2 to make tree with only root
BinaryTree(T ro, T le, T ri); // default constructor 3 to make complete binary tree
~BinaryTree(); // destructor for dynamics
BinaryTree(BinaryTree const& other) : root(other.root? new Tree_Node(*other.root) : 0) {}
bool isEmpty(); // method that returns t/f if tree is empty
T info(); // method to return value in root of the tree
void inOrder(); // traverses nodes in a tree left, root, right
void preOrder(); // traverses nodes in a tree root, left, right
void postOrder(); // traverses nodes in a tree left, right, root
private:
struct Tree_Node // represents a node
{
T data;
Tree_Node *left; // left pointer
Tree_Node *right; // right pointer
Tree_Node(T data, Tree_Node* left = 0, Tree_Node* right = 0)
: left(left), right(right) {}
Tree_Node(Tree_Node const& other)
: data(other.data),
left (other.left? new Tree_Node(*other.left) : 0),
right(other.right?new Tree_Node(*other.right) : 0)
{}
~Tree_Node()
{
delete left;
delete right;
}
};
Tree_Node *root; // create root with 2 pointers from this };
};
/***********************************************************************/
template<typename T> BinaryTree<T>::BinaryTree()
: root(0)
{
}
template<typename T> BinaryTree<T>::BinaryTree(T ro)
: root(new Tree_Node(ro, 0, 0))
{
}
template<typename T> BinaryTree<T>::BinaryTree(T ro, T le, T ri)
: root(new Tree_Node(ro,
new Tree_Node (le, 0, 0),
new Tree_Node (ri, 0, 0)))
{
}
template<typename T> BinaryTree<T>::~BinaryTree() {
delete root;
}
template<typename T> bool BinaryTree<T>::isEmpty()
{
return !root;
}
template<typename T> T BinaryTree<T>::info()
{
}
template<typename T> void BinaryTree<T>::inOrder()
{
}
template<typename T> void BinaryTree<T>::preOrder()
{
}
template<typename T> void BinaryTree<T>::postOrder()
{
}
#endif /* BINARYTREE_H */
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <ctime>
#include <limits>
//#include "BinaryTree.h"
//#include "stack.h"
using namespace std;
int main()
{
stack<BinaryTree<char> > testing;
BinaryTree<char> testing2('d', 'd', 'd');
testing.push(testing2);
cout << testing.size();
return 0;
}
enter code here
相关文章:
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- 换位表导致测试失败(但在游戏中运行良好)
- 为什么 KMS drmModeSetCrtc() 在 X11 会话中运行时会失败并被拒绝权限?
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 如何让谷歌测试正常运行。测试总是失败。(它不会编译)
- 运行时检查失败 #0 用于运行时重新编译
- 如果所有部分都失败,Catch2 将再次运行测试
- 为什么我的Qt程序在断言失败后继续运行?
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- 运行时检查失败 #2 MSVC 仅使用 utf8proc 进行调试
- std::copy 导致运行时检查失败 #2
- 运行时检查失败 #2 - 变量周围的堆栈'...'已损坏
- 对c++中使用的keras.h5模型到.pb模型进行了微调,给出了运行模型失败的错误
- Opendir() 在带有空格的名称上失败,在 Linux 上运行
- 运行时检查失败 #2 - 变量"l1"周围的堆栈已损坏
- C++:此代码可以编译,但引发运行时检查失败 #2 - 围绕变量周围的堆栈'num'已损坏。发生
- 运行时检查失败 - 变量周围的堆栈已损坏
- arm_data在第二次运行我的程序时中止失败,然后
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- 简单的for循环完成并失败运行