Re: Filestream problems
On Apr 17, 3:59 am, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
Hani Sharabash wrote:
#include "binarytree.h"
#include "treenode.h"
#include "datanode.h"
I read your code and I'm pretty sure the problem is in these files
somewhere. Please provide a minimal but complete example, i.e. one that
doesn't contain any unnecessary stuff but still contains everything that
one needs to reproduce the problem.
Uli
--
Sator Laser GmbH
Gesch?ftsf?hrer: Michael W?hrmann, Amtsgericht Hamburg HR B62 932
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Here are the contents of binarytree.cpp.
#include "binarytree.h"
BinaryTree::BinaryTree()
{
root = NULL;
}
void BinaryTree::insert(string s)
{
Treenode* newnode = new Treenode(s);
root = recursiveInsert(root, newnode);
}
Treenode* BinaryTree::recursiveInsert(Treenode* subroot, Treenode*
newnode)
{
if (subroot == NULL)
return newnode;
else if (newnode->getWord() < subroot->getWord())
subroot->left = recursiveInsert(subroot->left, newnode);
else if (newnode->getWord() > subroot->getWord())
subroot->right = recursiveInsert(subroot->right, newnode);
else
subroot->increaseWordCount();
return subroot;
}
void BinaryTree::walk()
{
if (root == NULL)
cout << "Empty Tree";
else
recursiveWalk(root);
cout << endl;
}
void BinaryTree::recursiveWalk(Treenode* subroot)
{
if (subroot == NULL)
return;
recursiveWalk(subroot->left);
cout << subroot->getWord() << " ";
recursiveWalk(subroot->right);
}
binarytree.h:
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include "treenode.h"
using namespace std;
class Treenode;
class BinaryTree
{
public:
BinaryTree();
void insert(string s);
Treenode* recursiveInsert(Treenode* subroot, Treenode* newnode);
void walk();
void recursiveWalk(Treenode* subroot);
private:
Treenode* root;
};
#endif
treenode.cpp:
#include "treenode.h"
Treenode::Treenode(string s)
{
data->word = s;
data->wordCount = 1;
left = right = NULL;
}
string Treenode::getWord()
{
return data->word;
}
int Treenode::getWordCount()
{
return data->wordCount;
}
void Treenode::increaseWordCount()
{
data->wordCount++;
}
treenode.h:
#ifndef TREENODE_H
#define TREENODE_H
#include <iostream>
#include "binarytree.h"
#include "datanode.h"
using namespace std;
class Treenode
{
friend class BinaryTree;
public:
Treenode(string s);
string getWord();
int getWordCount();
void increaseWordCount();
private:
Datanode* data;
Treenode *left, *right;
};
#endif
datanode.h (there is no datanode.cpp file):
#ifndef DATANODE_H
#define DATANODE_H
using namespace std;
class Datanode
{
friend class Treenode;
private:
string word;
int wordCount;
};
#endif
Thank you so much for your help.
On Apr 17, 3:56 am, Carl Barron <cbarron...@adelphia.net> wrote:
In article
<73c37290-e418-4d12-934a-c33075210...@m36g2000hse.googlegroups.com>,
Hani Sharabash <HaniB...@gmail.com> wrote:
I'm writing a program in which I read text from a file, store all of
the words into a binary search tree, and then print the words out to
the screen in order from that tree.
For some reason, opening and using an input filestream is causing
problems for my binary tree functions. When I try to use them
together, my program crashes at runtime. For example:
\
first I would make sure where the error is in this case I would
imput the data and store it in a stanard sorted assoc. container
say std::set<std::string> and print the results from
std::set<std::string> before I blame <fstream> probably there is
a bug in your binary tree code. But if reading it into an
std::set<string> works then its not <fstream>.
I don't understand. Could you clarify a little more?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]