问题描述
int main()
{
HeapTree* root = NULL;
HeapTree object;
ifstream myFileStream("kisi.txt");
if (!myFileStream.is_open())
{
cout << "kisi.txt dosyasi bulunamadi" << endl;
}
string isim,yass,kiloo,line;
int yas,kilo;
while (getline(myFileStream,line))
{
stringstream ss(line);
getline(ss,isim,'#');
getline(ss,'#');
stringstream ss2(yass);
ss2 >> yas;
stringstream ss3(kiloo);
ss3 >> kilo;
yas = 2020 - yas;
root = insert(root,yas,kilo,isim);//here is my problem i cant call insert
}
myFileStream.close();
object.postorder(root);
return 0;
}
#ifndef HEAPTREE_HPP
#define HEAPTREE_HPP
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include <stack>
using namespace std;
class HeapTree
{
public:
int key;
HeapTree* left;
HeapTree* right;
int height;
int yas;
int kilo;
string isim;
stack <char> s;
int max(int a,int b);
HeapTree* insert(HeapTree*,int,string);
void postorder(HeapTree*);
~HeapTree();
};
#endif
#include "HEAPTREE.hpp"
int height(HeapTree* N)
{
if (N == NULL)
return 0;
return N->height;
}
int max(int a,int b)
{
return (a > b) ? a : b;
}
HeapTree* newNode(int key,int kilo,string isim)
{
HeapTree* Heaptree = new HeapTree();
Heaptree->key = key;
Heaptree->left = NULL;
Heaptree->right = NULL;
Heaptree->height = 1;
Heaptree->s.push('O');
Heaptree->isim = isim;
Heaptree->kilo = kilo;
return(Heaptree);
}
HeapTree* rightRotate(HeapTree* y)
{
HeapTree* x = y->left;
HeapTree* T2 = x->right;
x->right = y;
y->left = T2;
int yHeight = y->height;
int xHeight = x->height;
y->height = max(height(y->left),height(y->right)) + 1;
x->height = max(height(x->left),height(x->right)) + 1;
if (x->height > xHeight)
x->s.push('Y');
else if (x->height < xHeight)
x->s.push('A');
else
x->s.push('D');
if (y->height > yHeight)
y->s.push('Y');
else if (y->height < yHeight)
y->s.push('A');
else
y->s.push('D');
// Return new root
return x;
}
HeapTree* leftRotate(HeapTree* x)
{
HeapTree* y = x->right;
HeapTree* T2 = y->left;
y->left = x;
x->right = T2;
int yHeight = y->height;
int xHeight = x->height;
x->height = max(height(x->left),height(x->right)) + 1;
y->height = max(height(y->left),height(y->right)) + 1;
if (x->height > xHeight)
x->s.push('Y');
else if (x->height < xHeight)
x->s.push('A');
else
x->s.push('D');
if (y->height > yHeight)
y->s.push('Y');
else if (y->height < yHeight)
y->s.push('A');
else
y->s.push('D');
return y;
}
int getBalance(HeapTree* N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
HeapTree* insert(HeapTree* HeapTree,int key,string isim)
{
if (HeapTree == NULL)
return(newNode(key,isim));
if (key < HeapTree->key) {
HeapTree->s.push('Y');
HeapTree->left = insert(HeapTree->left,key,isim);
}
else if (key > HeapTree->key)
{
HeapTree->s.push('A');
HeapTree->right = insert(HeapTree->right,isim);
}
int nHeight = HeapTree->height;
HeapTree->height = 1 + max(height(HeapTree->left),height(HeapTree->right));
int balance = getBalance(HeapTree);
if (balance > 1 && key < HeapTree->left->key)
return rightRotate(HeapTree);
if (balance < -1 && key > HeapTree->right->key)
return leftRotate(HeapTree);
if (balance > 1 && key > HeapTree->left->key)
{
HeapTree->left = leftRotate(HeapTree->left);
return rightRotate(HeapTree);
}
if (balance < -1 && key < HeapTree->right->key)
{
HeapTree->right = rightRotate(HeapTree->right);
return leftRotate(HeapTree);
}
return HeapTree;
}
void HeapTree::postorder(HeapTree* root)
{
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
cout << root->isim << "," << 2020 - root->key << "," << root->kilo << " ";
while (!root->s.empty())
{
cout << root->s.top() << " ";
root->s.pop();
}
cout << endl;
}
HeapTree::~HeapTree() {}
此堆树程序在main.cpp类中有一个关于插入方法的问题,它使用它从txt文件中获取名称并进行一些操作,但是我的问题是我无法从HeapTree.cpp中调用它,我认为应该编写它在heaptree.hpp中定义它,我尝试一些方法来解决它,但是我想以一种专业的方式来解决它,我认为这是一个简单的问题。 谁能帮我
解决方法
在文件HeapTree.cpp
中定义函数时,没有什么可告诉编译器这些函数是HeapTree类的成员,而不是普通函数。
例如:
HeapTree* insert(HeapTree* HeapTree,int key,int kilo,string isim)
{
...
}
这定义了一个名为insert()
的函数,该函数返回一个HeapTree*
。函数insert()
未定义为HeapTree类的成员。
相反,您应该写:
HeapTree* HeapTree::insert(HeapTree* HeapTree,string isim)
{
...
}
请注意函数名称前面的多余HeapTree::
。您可以将此前缀添加到文件HeapTree.cpp
中每个函数的定义中,以告诉编译器这些不是函数,而是类HeapTree的成员。