我正在尝试使用递归函数将二叉树写入文件。调用writeFile(std::string fileName)
打开文件流,然后将其传递给递归函数调用,writeFile(BinNode<T> ptr, std::ofstream outFile)
. 当第一个函数调用第二个函数时,编译器返回错误“尝试引用已删除的函数”(在我的代码中注明的行。我研究了错误代码,我有一个默认构造函数,一个析构函数,所以没有一个函数是隐含的任何想法将不胜感激。
BinTree.h:
#ifndef BINTREE_H
#define BINTREE_H
#include <iostream>
#include <fstream>
#include "BinNode.h"
template<typename T>
class BinTree {
public:
BinTree(); // Default constructor
// Value semantics
// copy constructor
BinTree(const BinTree<T>& source);
// assignment operator
BinTree<T>& operator=(const BinTree<T>& source);
// destructor
~BinTree() { free(root); }
bool isEmpty();
void add(T item);
void printAll() { print(root); }
bool readFile(std::string fileName);
bool writeFile(std::string fileName);
private:
BinNode<T>* root;
void free(BinNode<T>* ptr);
void print(BinNode<T>* ptr);
void copyTree(BinNode<T>* ptr);
void writeFile(BinNode<T>* ptr, std::ofstream outFile);
};
#include "BinTree.template"
#endif
BinTree.template:
#include "BinTree.h"
// BinTree.template definition
template <typename T>
BinTree<T>::BinTree() {
root = nullptr;
}
template<typename T>
BinTree<T>::BinTree(const BinTree<T>& source) {
this->root = nullptr;
copyTree(source->root);
}
template<typename T>
BinTree<T>& BinTree<T>::operator=(const BinTree<T>& source) {
if (this == &source)
return *this;
free(this->root);
this->root = nullptr;
copyTree(source->root);
}
template<typename T>
bool BinTree<T>::isEmpty() {
return (root == nullptr); // true - empty, false - not empty
}
template<typename T>
void BinTree<T>::add(T item) {
BinNode<T>* temp = new BinNode<T>(item); // other constructor
if (isEmpty())
root = temp;
else {
BinNode<T>* ptr = root;
while (ptr != nullptr) {
if (temp->data <= ptr->data) { // Left side
if (ptr->left == nullptr) { // Left spot open
ptr->left = temp; // Add it
ptr = nullptr; // End Loop
}
else { // Left spot not open
ptr = ptr->left;
}
}
else { // Right side
if (ptr->right == nullptr) { // Right open
ptr->right = temp; // Add it
ptr = nullptr; // End Loop
}
else {
ptr = ptr->right;
}
}
}
}
}
template<typename T>
bool BinTree<T>::readFile(std::string fileName) {
std::ifstream inFS;
inFS.open(fileName);
T temp;
if (!inFS.is_open()) {
std::cout << "Error opening " << fileName << std::endl;
return false;
}
while (!inFS.eof()) {
getline(inFS, temp);
add(temp);
}
inFS.close();
return true;
}
template<typename T>
bool BinTree<T>::writeFile(std::string fileName) {
std::ofstream outFS;
outFS.open(fileName);
if (!outFS.is_open()) {
std::cout << "Error writing to " << fileName << std::endl;
return false;
}
writeFile(root, outFS); // <------ COMPILER ERROR ON THIS LINE ****
outFS.close();
return true;
}
template<typename T>
void BinTree<T>::writeFile(BinNode<T>* ptr, std::ofstream outFile) {
if (ptr != nullptr) {
writeFile(ptr->left, outFile);
outFile << ptr->data << std::endl;
writeFile(ptr->right, outFile);
}
}
template<typename T>
void BinTree<T>::free(BinNode<T>* ptr) {
if (ptr != nullptr) {
free(ptr->left);
free(ptr->right);
delete ptr;
}
}
template<typename T>
void BinTree<T>::print(BinNode<T>* ptr) {
if (ptr != nullptr) {
print(ptr->left);
std::cout << ptr->data << std::endl;
print(ptr->right);
}
}
template<typename T>
void BinTree<T>::copyTree(BinNode<T>* ptr) {
if (ptr != nullptr) {
add(ptr->data);
copyTree(ptr->left);
copyTree(ptr->right);
}
}
BinNode.h:
#ifndef BINNODE_H
#define BINNODE_H
template <typename T>
class BinTree;
template <typename T>
class BinNode {
friend class BinTree<T>;
public:
BinNode() : left(nullptr), right(nullptr), data(T()) {} // Default constructor
BinNode(T item) : left(nullptr), right(nullptr), data(item) {}
private:
BinNode<T>* left;
BinNode<T>* right;
T data;
};
#endif