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

5
  • list<Person*> vs list<Person>. Notice the *. Commented Jan 2, 2017 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 Commented Jan 2, 2017 at 11:24
  • How should I initialize the list in the cpp file ? Commented Jan 2, 2017 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) Commented Jan 2, 2017 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? Commented Jan 2, 2017 at 14:05

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

8 Comments

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
Which error it prints? Maybe you forgot to initialize this static class variable in cpp unit?
I did this in both the header the the cpp files: #include <list> using namespace std; Then I wrote this: static list<Person*> 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)
Perhaps I didnt initialize the static list in the right way? ( In the cpp )
I updated answer according to fact, that group is global variable. And in this light this question is duplicate of stackoverflow.com/questions/1856599/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.