C++ pointer trouble

O

Octon22

When you have several different classes that interact, how do you
avoid compile errors when doing the following:

class Person
{
City* places; // array of pointers to cities he's been to
};

class City
{
vector <Person> population; // array of houses that are within this
city
};


the compiler gets to the definition of class Person and doesn't
recognize 'City' as a class. Functions don't have this problem
because you can declare prototypes before defining the function body.
How can I solve this problem when it comes to classes? Thanks.
 
S

Sharad Kala

Octon22 said:
When you have several different classes that interact, how do you
avoid compile errors when doing the following:

class Person
{
City* places; // array of pointers to cities he's been to
};

class City
{
vector <Person> population; // array of houses that are within this
city
};


the compiler gets to the definition of class Person and doesn't
recognize 'City' as a class. Functions don't have this problem
because you can declare prototypes before defining the function body.
How can I solve this problem when it comes to classes? Thanks.

What you require is called a forward declaration .

class City; // Forward declaration
class Person
{
City* places; // array of pointers to cities he's been to
};

Best wishes,
Sharad
 
D

David Harmon

When you have several different classes that interact, how do you
avoid compile errors when doing the following:

This issue is covered in the topic "[34.9] How can I create two classes
that both know about each other?" of Marshall Cline's C++ FAQ. It is
always good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
D

Dan Cernat

Octon22 said:
When you have several different classes that interact, how do you
avoid compile errors when doing the following:

class Person
{
City* places; // array of pointers to cities he's been to
^^^^^^^^^^^^^^^^^^^^ array of cities
City** places; // array of pointers to cities;
 
F

Frank Puck

Octon22 said:
When you have several different classes that interact, how do you
avoid compile errors when doing the following:

class Person
{
City* places; // array of pointers to cities he's been to
};

class City
{
vector <Person> population; // array of houses that are within this
city
};


the compiler gets to the definition of class Person and doesn't
recognize 'City' as a class. Functions don't have this problem
because you can declare prototypes before defining the function body.
How can I solve this problem when it comes to classes? Thanks.

#include <set>

class City; // forward declaration so the compiler knows what City stands
for.
class Person
{
// set is ideal for your task
std::set<City*> places;
}


Buy a book about STL!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top