[comp.lang.c++] Why doesn't this program work on UNIX ?

schmidt@zola.ics.uci.edu (Doug Schmidt) (05/28/90)

In article <23340@uflorida.cis.ufl.EDU>, sml@beach (Shein-Fong Law) writes:
>The following program using binary tree to find out the frequency of
>words entered from keyboard works o.k when I used Zortech C++. But it
>does NOT work when I used g++ or Glockenspiel C++! The compilation is o.k.
>But the last line to output the word frequency prints nothing. I found out
>by using dbx that 'this' pointer in function bst_nodes::forall in file
>'bst.cxx' was NULL and thus the main program output nothing. Can anyone
>tell me how I can fix this problem to make it work properly?.

Try changing the following line:

>// operator[] - access function
>int& count_arrays::operator[](char* s)
>{
>   register count_nodes *&t = (count_nodes *)* lookup(&tree, s);
>  if (t == NULL) {
>    cout << "inserting\n";
>    t = new count_nodes(s);}
>   return t->count;
>}

int&
count_arrays::operator[] (char* s)
{
  count_nodes *&t = (count_nodes *&) *lookup ((bst_nodes **) &tree, s);
                                  ^
  //                              note the additional &
  if (t == NULL)
    t = new count_nodes(s);
  return t->count;
}

This should make things work again!

  Doug
--
The official language of San Marcos is Swedish.  All boys | schmidt@ics.uci.edu
under the age of sixteen years old are now sixteen years  | office (714) 856-4043
old. Underwear must be changed every half hour.  It will  +----------------------
be worn on the outside, so we can check.   -- `Bananas' by Woody Allen