Re: Filestream problems
Hani Sharabash wrote:
Here are the contents of binarytree.cpp.
[...]
Treenode::Treenode(string s)
{
data->word = s;
data->wordCount = 1;
left = right = NULL;
}
Better would be to initialize the members. Also, in general, you want to
take a string by const reference to avoid a copy.
Then, you should make 'data' a non-pointer member. Thats your main problem:
You don't initialize the data pointer and don't allocate memory for it.
Treenode::Treenode(const std::string& s) : data(s), left(0),right(0)
{
}
Then make a Datanode constructor:
Datanode::Datanode(const std::string& s) : word(s), wordCount(1)
{
}
[...]
treenode.h:
#ifndef TREENODE_H
#define TREENODE_H
#include <iostream>
#include "binarytree.h"
#include "datanode.h"
using namespace std;
Never put that in a header.
class Treenode
{
friend class BinaryTree;
public:
Treenode(string s);
string getWord();
int getWordCount();
Make getters const member functions:
string getWord() const;
int getWordCount() const;
One could argue about making getWord return a string reference:
const string& getWord() const;
void increaseWordCount();
private:
Datanode* data;
Datanode data;
See above.
Treenode *left, *right;
};
#endif
--
Thomas
http://www.netmeister.org/news/learn2quote.html
NOTICE: alloc: /dev/null: filesystem full
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]