Archived

This topic is now archived and is closed to further replies.

Dynamic Array of Templated Classes

This topic is 4957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by ace_lovegrove
He could take the standard library one but then he wouldn''t know how it works necessarily


That would be okay if he was trying to learn data structures & algorithms but that doesn''t seem to be the case, even if it was once you''ve learnt the concept there is no point in making your own list you will end up writing more or less the same thing but crappy and less portable.

quote:

im an enemy of the standard library



Err why? i haven''t heard of something as ridiculous as this

Share this post


Link to post
Share on other sites
quote:
Original post by Tevalyn
nm, I think I got it, simple as...

linkedList lineList[95];

[edited by - Tevalyn on May 17, 2004 10:02:15 PM]


If your linked list is correct your making 95 linked list of integers is that what your trying to do? by the way do you actually need to use your own linkedlist because your most probably better off using standard libaray list

[edited by - snk_kid on May 18, 2004 3:21:20 AM]

Share this post


Link to post
Share on other sites