views:

1712

answers:

5

Some code for context:

class WordTable
{
public:
 WordTable();
 ~WordTable();

 List* GetListByAlphaKey(char key);
 void AddListByKey(char key);
 bool ListExists(char key);
 bool WordExists(string word);
 void AddWord(string word);
 void IncrementWordOccurances(string word);
 void Print();
private:     
    List *_listArray[33];
    int _GetIndexByKey(char key);
};



class TableBuilder
{
public:
 TableBuilder();
 ~TableBuilder();
 void AnalyzeStream(fstream &inputStream);        
    void PrintResults();
private:
    void _AnalyzeCursor(string data);
    bool _WordIsValid(string data);
    WordTable* _WordTable;        
};

struct Element {
public:
   string Word;
   int Occurances;
   Element* Next;
};



class List
{
public:
 List();
 ~List();

 Element* AddElement(string word);  
 void DeleteElement(Element* element);  
 void Print();  
 void Delete();
 Element* First;
 bool WordExists(string word);
 void IncrementWordOccurances(string word);  
private:
 void _PrintElementDetails(Element* element);


};

Requirements
I must analyze text, building array of linked lists (where array contains list for each letter; list contains every word found in text), then print out results.

Problem I can`t initialize array of lists in WordTable.cpp. I know that i've misunderstood something, but i got no ideas and time. Anyone?

P.s. Yeah, that's a homework. STOP giving me advices about best practices, please... :)

+4  A: 

An initialization for _listArray would look like this:

WordTable::WordTable() {
  for (int i=0; i<33; i++)
    _listArray[i] = new List();
}

You don't really say what exactly the problem is so I'm not sure if this helps...

sth
Note: Use of magic numbers (33: what does it stand for??) is usually a bad idea.
Joce
Not always hard coding something is bad. 33 are letters of alphabet. I'm quite sure they won`t change next few days until my homework will be delivered. :)
Arnis L.
sth, sorry, this one didn`t help. I tried it already. Throws segmentation error.
Arnis L.
It is considered good practice to declare any constant numbers usingconst int letters = 33;or#define letters 33just to improve readability and maintainability
Stowelly
This answer is correct with the code you've given. Perhaps you need to check what List's constructor does, if this code crashes.
James Hopkin
@strebla: If you want a less generic answer it might be a good idea to describe the problems you're having more precisely than "I can't initialize it". No point in letting us guess what you already did and didn't try...
sth
Arnis: oh yes, hard-coding magic numbers is *always* a bad idea, if only for readability reasons.
Konrad Rudolph
No need to teach me design basics. I'm not guru at this (design things), but i didn't read anything new about that here either. I'm new at c++ but not at programming in general. Anyway - problem is solved. TableBuilder didn't initialized WordTable in constructor. Thank you guys...
Arnis L.
+3  A: 

It's because you're creating an array of pointers to lists. Either change it to List _listArray[33]; or initialize it like so:

for ( int i = 0; i < 33; ++i ) {
    _listArray[i] = new List();
}

// and to access methods
_listArray[i]->AddElement( "word" );
kitchen
This is the problem, initialization in this way throws an error right after it tries to use indexer on _listArray.
Arnis L.
Could you post the definition of the List class, instead of just the declaration?
kitchen
+3  A: 

It appears you are optimizing your linked lists by having an array of them, one for each first letter of the word. Don't do that.

Use std::map. Where the string is the word and the int is your count.

EDIT: If you ignore my advice... As has been pointed out your _listArray is actually an array of pointers, not an array of objects. I think you wanted an array of objects. Since the array is fixed length, and List has a default constructor, the simplest way to do that is to just say

List _listArray[33];

If you want dynamic allocation, you could do this instead:

List* _listArray;

And in the constructor:

_listArray = new List[33];

And in the destructor:

delete[] _listArray;
jmucchiello
While true, this does not adresses the poster's question re: how to initialize a linked list table.
Joce
Arguably, one should not be making linked lists in C++. The STL implementation of List will work as well as or better than a hand spun linked list. Additionally the use for the linked list here is dubious as the poster intends to search it often and a linked list is not a proper data structure for frequent search. Simply answering the user's question is of less value than helping the user find a solution.
jmucchiello
"It appears you are optimizing your linked lists by having an array of them, one for each first letter of the word. Don't do that." - This is exactly what i'm trying to do. Sadly that's one of the rules of my homework - to use self-made array of linked lists.
Arnis L.
Arnis L.
You should have said it was homework and you should have said you HAD to use an array of linked lists
jmucchiello
Sorry for wasting your time. And it was one of my first questions here on SO.
Arnis L.
A: 

What you describe does not match your code.

List *_listArray[33]; does not define an array of linked lists. It defines an array of pointers to List.

Shmoopty
I'm quite sure that our teacher would call 'pointers to list' as 'dynamic array'. That would fit, since pointers helps with size of the list.
Arnis L.
A: 

Please don't reinvent the wheel (again) Use std::list it works correctly, is safer, is standard.

Second : you have design problems : element attribute in list is public, but you have a private method ???.

Reinventing the wheel is what he needs - that`s a homework. dooh.
Arnis L.
And if is a homework, please when you describe the problem , describe exactly the requirements : for example : extract words from a text without using any c++ library.
Sorry for wasting your time... :)
Arnis L.