View previous topic :: View next topic |
Author |
Message |
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Thu Oct 31, 2002 9:38 pm Post subject: using templated "list" structure with custom class |
|
|
Hey all, this is a pretty simple question, probably, but it's been giving me hours of trouble....
I'm trying to use STL in a simple C++ program that holds a collection of "animal" objects (from another class animal.h that I wrote)...
I took the various online examples of making a list of ints:
Code: |
list<int> myList;
list<int>::iterator listi;
|
..and I'm attemting to modify this for use with animal objects:
Code: |
list<animal> Zoo;
list<animal>::iterator listIndex = Zoo.begin();
|
However, this fails, and seems to complain about the iterator. I can successfully create the Zoo, but when I try to make the iterator, it fails.
Am I doing this wrong? |
|
Back to top |
|
|
rac Bodhisattva
Joined: 30 May 2002 Posts: 6553 Location: Japanifornia
|
Posted: Fri Nov 01, 2002 1:44 am Post subject: |
|
|
Can you post some stripped-down compilable code that we can use to reproduce your errors? Only think I can think of from what you've said so far is that maybe the definition of type "animal" is not visible when trying to instantiate a list<animal>. _________________ For every higher wall, there is a taller ladder |
|
Back to top |
|
|
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Sun Nov 03, 2002 12:21 am Post subject: |
|
|
rac wrote: | Can you post some stripped-down compilable code that we can use to reproduce your errors? Only think I can think of from what you've said so far is that maybe the definition of type "animal" is not visible when trying to instantiate a list<animal>. |
rac, here's some code from zoo.cpp where I think the compiler is giving errors:
Code: |
zoo.cpp
---------
void zoo::reportStatus() {
list<animal>::iterator listIndex;
listIndex = park.begin();
while(listIndex <= park.end()) {
listIndex->printStatus();
}
}
|
And here's my entire zoo.h file:
Code: |
zoo.h
-------
#include <list>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "animal.h"
using namespace std;
#define MAXCOORD 5;
class zoo {
public:
zoo();
~zoo();
void repopulate();
void moveAllAnimals();
void reportStatus();
int randPercent();
int randCoord();
private:
void addAnimal();
list<animal*> park;
};
|
In animal.h I've got some more code, but I'm not sure it's relevant here, besides the fact that each animal has a reportStatus() function, used in the first code snippet. |
|
Back to top |
|
|
rac Bodhisattva
Joined: 30 May 2002 Posts: 6553 Location: Japanifornia
|
Posted: Sun Nov 03, 2002 6:31 am Post subject: |
|
|
The problem is that listIndex is a list<animal>::iterator, and park is a list<animal *>. Either make listIndex a list<animal *>::iterator, or make park a list<animal>, depending on how you want to manage your menagerie. _________________ For every higher wall, there is a taller ladder |
|
Back to top |
|
|
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Sun Nov 03, 2002 6:17 pm Post subject: |
|
|
rac wrote: | The problem is that listIndex is a list<animal>::iterator, and park is a list<animal *>. Either make listIndex a list<animal *>::iterator, or make park a list<animal>, depending on how you want to manage your menagerie. |
That was such a simple error, yet it's been troubling me for DAYS!
Thank you so much for clearing this up... now I can actually finish this project! |
|
Back to top |
|
|
|