以下代码的运行失败。
这是我的代码:
#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;
}