0

I have a class named Person, which contains of a header and a cpp files. I tried to declare a static list in the header file, like this:

 static list<Person*> group;

( I already included list in both the cpp and the header file). I've tried to write a Print function in the cpp file:

 void Person::PrintAll() const {
 int counter=0;
 list<Person>::iterator i;
 **for ( i= group.begin(); i != group.end(); ++i)**{
     cout << *i << endl;
     counter++;
 }

But I get lots of errors in the "for" line ( the one which is marked). I also tried to declare the static list in the cpp file, but I get the same error. What do I do wrong? Thanksalot

  • `list` vs `list`. Notice the `*`. – BoBTFish Jan 02 '17 at 11:12
  • Thanks for your quick repsonse. Unfortunately it still doesnt work. I think that the list is somehow not recognziable.. I tried to do this action: group.push_front(x); and I get this error :undefined reference to Person::group Perhaps I didnt decalre/intialized the list in a good manner? Thanks in regard – Shachar Wild Jan 02 '17 at 11:24
  • How should I initialize the list in the cpp file ? – Shachar Wild Jan 02 '17 at 11:58
  • I read your text as if group is a global variable? Since you are talking about if you could declare it in the CPP? If that is the case, may I ask why you didn't simply made it a class attribute? That said, have you tried accessing it another way? Like cout << *(group.back()) << endl;? Or what happens if you iterate over unsigned int to size instead of using an iterator? Is group properly initialized? (since you are talking about the for line being the problem, I assume that Person has a valid ofstream operator, but also state if that is the case) – Aziuth Jan 02 '17 at 13:32
  • Yes, person has a valid ofstream operator. Im no longer getting that error, but when I tired to do that in the main: Person:: PrintAll() I get these erros: - Invalid arguments ' Candidates are: void printAll() ' - cannot call member function ‘void Person::printAll() const’ without object Do u have any idea why this happens? – Shachar Wild Jan 02 '17 at 14:05

1 Answers1

1

list<Person>::iterator i is not an iterator to iterate over list<Person*>.

Your type should be Person*

Because group is global variable (as author mentioned in comments), there is two variants:

declare it:

.h:

extern std::list<Person*> group;

cpp:

std::list<Person*> group;

then it will be possible to use it across multiple cpp files.

Or if you want to use it just in one cpp file, then do not declare in header at all, and declare it in cpp file as static:

cpp:

static std::list<Person*> group;
Arkady
  • 2,084
  • 3
  • 27
  • 48
  • Thanks for your quick repsonse. Unfortunately it still doesnt work. I think that the list is somehow not recognziable.. I tried to do this action: group.push_front(x); and I get this error :undefined reference to Person::group Perhaps I didnt decalre/intialized the list in a good manner? Thanks in regard – Shachar Wild Jan 02 '17 at 11:20
  • Which error it prints? Maybe you forgot to initialize this static class variable in cpp unit? – Arkady Jan 02 '17 at 11:29
  • I did this in both the header the the cpp files: #include using namespace std; Then I wrote this: static list group;(right underneath the "using namespace std:" line) When I try to do this action: group.push_front(x) I get the " un defined reference to Person::group (And still getting the error in the PrintAll function) – Shachar Wild Jan 02 '17 at 11:32
  • Perhaps I didnt initialize the static list in the right way? ( In the cpp ) – Shachar Wild Jan 02 '17 at 11:37
  • I updated answer according to fact, that `group` is global variable. And in this light this question is duplicate of http://stackoverflow.com/questions/1856599/when-to-use-static-keyword-before-global-variables – Arkady Jan 02 '17 at 13:06
  • Ok I get no erros now. Thanks alot for your help ! :) – Shachar Wild Jan 02 '17 at 13:08
  • Im no longer getting that error, but when I tired to do that in the main: Person:: PrintAll() I get these erros: - Invalid arguments ' Candidates are: void printAll() ' - cannot call member function ‘void Person::printAll() const’ without object Do u have any idea why this happens? – – Shachar Wild Jan 02 '17 at 14:38
  • @ShacharWild, you can't call object's functions if you don't have class instance or if that function is not static. To call it like that: `Person:: PrintAll()` you should declare it as static function. But that becomes absolutely useless for other users of community, and it looks like you just need to learn more basis about C++. Try to figure out what is class, what is class member function, what is static class member function, what is local/global variable. And I guess you should delete entire this question :-) Or accept my answer, but I guess it's also useless for others. – Arkady Jan 02 '17 at 17:35