Dynamic Array of Templated Classes

Started by
11 comments, last by Tevalyn 19 years, 11 months ago
Hi, I am working on a program where I need an array of classes. The class I am working with is a basic linkedList, but I need the amount of them to be dependant on the length of a file. This is what I have so far... int *lineNums; int words = countWords(); linkedList lineList; lineNums = new linkedList[words]; Any help, guidance or hints would be much appreciated.
Advertisement
oops, forgot to add that this is using Microsoft Visual C++

Is this even possible? Ive read a few posts where it was done with non templated classes, but no matter where I put the I get an error
Do you actually need to make your own linked list? you can use the standard libary list which can take any type and is implemented as a doublely linked list

[edited by - snk_kid on May 17, 2004 5:49:24 PM]
quote:Original post by Tevalyn
Hi, I am working on a program where I need an array of classes. The class I am working with is a basic linkedList, but I need the amount of them to be dependant on the length of a file. This is what I have so far...

int *lineNums;
int words = countWords();

linkedList lineList;
lineNums = new linkedList[words];

Any help, guidance or hints would be much appreciated.


You can''t assign a pointer to an integer with a object of type linkedlist, i think what your trying to do is:

#include <cstdlib>#include <list>int main() {    int num_of_words = countWords();    std::list<int> my_ints(num_of_words); //Creates a list n elements    //what everelse ..    return EXIT_SUCCESS;}
The problem I am working on is basically a mini word processor, it reads a file, stores words and their line numbers. I was originally going to used a doubly linked list. But it can only store one quantity of data, so I was going to create a node in main and have a pointer point to it and store that pointer in the info space of the doublyLinkedList.

Couldnt get any of that to work. So what I am doing now is parallel linkedLists, one list will work like normal and have the word in its storage. The other will be an array, lineList[0] will store line numbers for the word that will be popped first, lineList[1] will store for the second and so on.

I just need to figure out how to make them arrayed dependant on a variable.
Yeah, that looks exactly like what im trying to do.

Thanks
quote:Original post by Tevalyn
The problem I am working on is basically a mini word processor, it reads a file, stores words and their line numbers. I was originally going to used a doubly linked list. But it can only store one quantity of data, so I was going to create a node in main and have a pointer point to it and store that pointer in the info space of the doublyLinkedList.

Couldnt get any of that to work. So what I am doing now is parallel linkedLists, one list will work like normal and have the word in its storage. The other will be an array, lineList[0] will store line numbers for the word that will be popped first, lineList[1] will store for the second and so on.

I just need to figure out how to make them arrayed dependant on a variable.


I haven't had any exprince with writing a word processor although i have read GOF book that describes a way to create one using patterns.

If your only going to store words and lines then one way you could do it quite easily is to have a list a of list of strings where each string is word and each list of list is a line etc e.g

//include ur headers here#include <cstdlib>#include <list>#include <string>int main() {     const int num_of_lines = get_num_of_lines();     std::list< std::list< std::string > > my_lines(num_of_lines);     for(int i = 0; i < num_of_lines; i++) {          int num_of_words = get_num_of_words();		  std::list<std::string> my_words(num_of_words);          for(int j = 0; j < num_of_words; j++)  {			 //read/parse a word into variable word                          std::string word = get_word();			  my_words.push_back(word);		  }		  my_lines.push_back(my_words);	 }	 return EXIT_SUCCESS;}


[edited by - snk_kid on May 17, 2004 6:23:58 PM]
He could take the standard library one but then he wouldn''t know how it works necessarily, im an enemy of the standard library

Ace
Why list success words? Why not pages, paragraphs, or sentences? You could push the words of a paragraph into a vector, whose access will be much faster (linear), and link these vectors. There are other options aswell.

blah blah blah.
quote:Original post by ace_lovegrove
He could take the standard library one but then he wouldn''t know how it works necessarily, im an enemy of the standard library

Ace


Because you''re a fool.

blah blah blah.

This topic is closed to new replies.

Advertisement