Assigning values to classes nested 3/4 deep

  • Thread starter John O' Meara
  • Start date
  • Tags
    Classes
In summary, the program should generate a table of words. However, when making the concordance, it gives the wrong output.
  • #1
John O' Meara
330
0
For the program segment please see attachment.
THe program compiles and runs but gives the worng answer; its output is suppose to be like below, if you type in "Our second version of table" on a single line you should get:

previous words following line no.
Our second 0
Our second version 0
second version of 0
version of table 0
of table 0

Instead I get:

0xe59ff2b8 Our 0xe59ff2b8 123456789
0xe59ff2b8 second 0xe59ff2b8 123456789
0xe59ff2b8 version 0xe59ff2b8 123456789
etc.,

The program should generate a table of words. When making this concordance I want to gather information regarding the context of each word as well as its line number. To get a context for a word just store its previous word and its following word.
The principle of assigning pointers in the function setLists() may be wrong, it is just that I am out of ideas at the moment. I tried scanning the all code into a rich text format but it didn't process it that well so I decided to just type the revalent parts of the program into a word document instead. If you want the whole program I can attach it as pdf later, just say so. Thanking you in advance for the help.
 

Attachments

  • table.doc
    40.5 KB · Views: 216
Physics news on Phys.org
  • #2
For the benefit of other readers-
Code:
int line = 0;
const int maxSize = 10;
const int arraySize = 20;  // max size of a word

class  charList {
         char *value;
         charList *nextEntry;
     public:
        charList(char *v,  charList *next) : value(v), nextEntry(next) {}
        char *getValue() { return value; }
        charList *getNextEntry() { return nextEntry; }
       void print()
       {
           cout << value << ‘\n’;
           if (nextEntry != NULL)
             nextEntry->print();
       }
 };
  class intList {
           int value;
           intList *nextEntry;
         public:
           intList(int v, intList *next) : value(v), nextEntry(next) { }
           int getValue() { return value; }
           intList *getNextEntry() { return nextEntry;  }
  };
   class lists {
            intList *front;
            charList *previous, *following;
       lists *nextEntry;
     public:
        lists(charList *p, charList *f, intList *l, lists *next)
        : previous(p), following(f), front(l), nextEntry(next) { }
         charList*getPrevious() { return previous; }
         charList *getFollowing() { return following; }
         intList *getFront() { return following; }
         intList *enterInt(int x);
         charList *enterChar(char *s);
         void setInt(int i, intList *l) { l = enterInt(i); }
         void setChar(char *str, charList *l) { l = enterChar(str); }
 };
  intList *lists::enterInt(int x)
  {
     intList *list = NULL;
     list = new intList(x, list);
     return list;
   }
   charList *lists::enterChar(char *s)
   {
      charList *list = NULL;
       list = new charList(s, list);
       return list;
    }
 class word {
          char *theString;
           int theFrequency;
           lists ptr[maxSize];
           lists *p;
    public:
        word() : theString(NULL), theFrequency(0) { }
        word(char *w) : theString(w), theFrequency(1) { }
        char *getString() { return theString; }
        int getFrequency() { return theFrequency; }
        void incrFrequency() { theFrequency++; }
        void resetP() { p = *ptr; }
        lists *getP() { return p; }
        void incrP() { p++; }
        void setLists(char *sp, charList *lp, char *sf, charList *lf,
                              int I, intList *l, lists *pt);
 }

        void word::setLists(char *sp, charList *lp, char *sf, charList *lf,
                              int I, intList *l, lists *pt)
        {
           pt = new lists(lp, lf, l, NULL);
           pt->setChar(sp , lp);
           pt->setChar(sf, lf);
           pt->setInt(i, l);
           cout << pt->getPrevious() << ‘ ‘ << pt->getFollowing() << ‘ ‘
                   << pt->getFront() << ‘\n’;
// Problem here: The computer returns the same address for both previous and 
//following namely 0xe59ff2b8 and for getFront() a number 23156
// 2nd: For every word entered, I get the same address for each word’s previous and
// following pointers namely; 0xe59ff2b8 and the same number for front 23156
      }
  ostream &operator<<(ostream &os, word *w)
  {
     For (int i = 0; i < w->getFrequrncy(); i++)
         os << setw(10) << w->getP()->getPrevious() << setw(10) << w->getString()
              << setw(10) << w->getP()->getFollowing() << setw(4) 
              << w->getP()->getFront() << ‘\n’;
        return os;
   }
 class wordlist {
          word *value;
          wordlist *nextEntry;
         public:
           wordlist(word *v, wordlist *next)
           : value(v), nextEntry(next) { }          
           word *getValue() { return value; }
           wordlist *getNextEntry() { return nextEntry; }
           void print()
            {
               cout << value << ‘\n’;
               if (nextEntry != NULL)
                    nextEntry->print();
             }
  };
   ostream &operator<<(ostream &os, wordlist *w)
   {
       w->print();
        return os;
    }
const int maxTableSize = 20000;

 class table {
            wordlist *words[maxTableSize];
            int size;
       public:
           table() : size(0) { }
           void addOccurence(char *str)
           {
               word *w = new word(str);
               int = 0;
               
               while (i < size) { // add w as new entry
                    words[size] =  new wordlist(w, NULL);
                     if (i == size) {   // the first word in wordlist
                         words[size]->getValue()->
                              setLists(NULL,
                                            words[size]->getValue()->getP()->getPrevious(),
                                            NULL,
                                            words[size]->getValue()->getP()->getFollowing(),
                                            line,
                                            words[size]->getValue()->getP()->getFront,
                                            words[size]->getValue()->getP());
                    }
                    else {
                       words[size]->getValue()->
                           setLists(words[size]->getValue()->getString(),
                                         words[size]->getValue()->getP()->getPrevious(),
                                         NULL,
                                         words[size]->getValue()->getP()->getFollowing(),
                                         line,
                                         words[size]->getValue()->getP()->getFront,
                           words[size]->getValue()->getP());
          }
          size++;
   }
    else { // word already in table more code here…
   }
 }
  void listEnteries();
 };
 void table::listEnteries()
 {
    For (int i = 0; i < size; i++)
       cout <<  words[i] << ‘\n’;
  }
int main()
{
   char *word;
   table concordance;
    
   word = readword(); // readword() function not given
  while (word != NULL) {
     concordance.addOccurence(word);
     word = readword();
    }
    concordance.listEnteries();
 return 0;
}
 
Last edited:
  • #3
Thanks Mark44, I don't know how you did it and so fast, but the code looks a lot better now. Just to make the code more understandable and why it is structured the it is. I should show you the following tables:
lists ** previous words following line no.
ptr[0] only in that 2
ptr[1] maintained in increasing 3
ptr[2] order in the 4

'previous', 'following' and 'line' are single entry lists held in member table::wordList::word::lists; and therefore pointed to by a ptr pointer, this allows us to sort according to previous, following or line number and swap the whole lot by just swapping pointer with each other; words is held in the table::wordList class. As just said the above is so ordered to allow the pointer-to-pointer of ...lists to be sorded according to the 'previous' word or the 'following' word or the line number in which words occurs in. So if you sort according to the previous word you get:

ptr[1] maintained in increasing 3
ptr[0] only in that 2
ptr[2] order in the 4

But first I have to get data into the pointers ptr[0], ptr[1], ptr[2], etc.
 
  • #4
It's hard to comprehend what you're doing, since you have so few comments (I counted four, other than the ones where you indicate you're having a problem). Also, some of your variables and method names don't give the reader a clear idea of what they're used for. For example, you have a word class that contains a string (e.g. "maintained in increasing", a frequency, an array of lists, and a pointer to lists. Maybe I am misunderstanding, but that seems more like a phrase than a word to me.

Are you asking how to initialize your table? That seems to be happening when you call addOccurrence(word).

Since you are getting the same address back for previous and following, there are three possibilities:
1) What you're pointing to is set up correctly, but your code for getPrevious and/or getFollowing is buggy, and you get incorrect values.
2) What you're pointing to is NOT set up correctly, and your code for getPrevious and/or getFollowing is as it should be, but it faithfully reports incorrect values.
3) What you're pointing to is NOT set up correctly, AND your code for getPrevious and/or getFollowing is buggy.

Since you are writing code this complex, you really should be using a debugger. It would also be helpful to draw some diagrams of what the pointers are supposed to be pointing to.

Put a breakpoint in addOccurrence and watch what variables get set as you single-step through the code of this function. That's probably the best advice I can give short of trying to run the code myself.
 
  • #5
I have attached a two page pdf document, which gives an overview of what I am trying to do and the structure of the program in terms of five simple classes . You may tell me that what I'm trying to do in terms of the structure of the program is too complicated or not possible. But your help is very much appreciated in any event, and thank you for it.
The code piece that you have is incomplete an so will not compile. If you want to run it on your computer I will send on the complete code in pdf format, as I am using g++ on a RISC OS platform, but it has no debugger with it. I hope the attachment helps make more clear the structure of the code at least. The conclusion I come to at the end of the attachment is, I am trying to access a member of a class of which no instance of exists and no pointer can point to these members then. Thanks again, for your time.
 

Attachments

  • Scan_Doc0007.pdf
    1.1 MB · Views: 320
  • #6
John, my guess is that there is a debugger, possibly gdb (Gnu Debugger) for your processor architecture. It would be well worth your while to do a search for a debugger, and then learn how to use it. You would have to spend some time reading the documentation for the debugger, but consider that an investment that will pay off by greatly reducing the amount of time you spend scratching your head, wondering why your code isn't producing the results you expect.

The structure you have is complicated, but I wouldn't say it's too complicated. BTW, your description in the PDF is a little off - you don't have classes nested inside classes. What you have is classes with pointers to other classes, something like this. A has a pointer to B. B has a pointer to C, and so on.

What I would advise is to draw a diagram of your classes. Draw each class as a box, with boxes inside it for the data members (especially the pointers). For each data member that points to another class, draw a curvy line with an arrow at the end, pointing to whatever class it points to (i.e., contains the address of). Having a diagram will help when you write expressions with pointer variables, to make sure you're pointing to what you think you are pointing to.

What your code needs to do is initialize heap memory for the bottom-most class instances, and then initialize the pointers appropriately all the way up. In the current layout, that would happen in addOccurrence.
 
  • #7
Are you running on Windows? If so you might want to try Visual Studio Express C / C++, which is free and includes a source level debugger. I recall being able to compile your previous code examples using Visual C without any issues.
 
  • #8
Hi rcgldr,
no I am not running my code on windows, but I also have a windows laptop. It is an idea to get it for the lp and use that as a backup when I run into trouble again. I will find it using a Google search? Thank you, rcgldr.
Mark44, thank you for all your replies. Yes, "nested classes" was definitely not the correct description; as the code is about pointers to classes and trying to assign these pointers. I hope I didn't waste your time looking for an example of a nested class in the original post. I don't how I got into my head I was dealing with nested classes, I know one can spend considerable time looking through code coloured by what one was told about it. Thank you again.
 

Related to Assigning values to classes nested 3/4 deep

1. How do I assign a value to a class that is nested 3 or 4 levels deep?

In order to assign a value to a class that is nested 3 or 4 levels deep, you will need to use the appropriate selector in your CSS code. For example, if you have a class called "outer" nested within a class called "middle", which is then nested within a class called "inner", you can assign a value to the "outer" class by using the selector ".inner .middle .outer" in your CSS code.

2. What is the purpose of nesting classes 3 or 4 levels deep?

Nesting classes 3 or 4 levels deep can be useful for organizing and styling complex layouts or elements. It allows for more specific targeting of elements and can help to avoid conflicts with other styles on the page.

3. Can I assign multiple values to a class that is nested 3 or 4 levels deep?

Yes, you can assign multiple values to a class that is nested 3 or 4 levels deep by using the appropriate selectors in your CSS code. Each value will apply to the corresponding nested element.

4. Are there any limitations to nesting classes 3 or 4 levels deep?

While nesting classes 3 or 4 levels deep can be useful, it is important to avoid excessive nesting as it can lead to overly complex and difficult to maintain code. It is also important to consider the performance implications of deeply nested classes, as it can impact the loading speed of your website.

5. How can I override styles for a class nested 3 or 4 levels deep?

In order to override styles for a class that is nested 3 or 4 levels deep, you can use the !important declaration in your CSS code. This will give the specified style priority over any other styles that may be applied to the same element.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
979
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
Back
Top