C++ pointer trouble

Discussion in 'C++' started by Octon22, Jan 17, 2004.

  1. Octon22

    Octon22 Guest

    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.
     
    Octon22, Jan 17, 2004
    #1
    1. Advertising

  2. Octon22

    Sharad Kala Guest

    "Octon22" <> wrote in message
    news:...
    > 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
     
    Sharad Kala, Jan 17, 2004
    #2
    1. Advertising

  3. Octon22

    David Harmon Guest

    On 16 Jan 2004 22:41:52 -0800 in comp.lang.c++,
    (Octon22) was alleged to have written:
    >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/
     
    David Harmon, Jan 17, 2004
    #3
  4. Octon22

    Dan Cernat Guest

    "Octon22" <> wrote in message
    news:...
    > 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;

    > };
    >
    > 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.
     
    Dan Cernat, Jan 17, 2004
    #4
  5. Octon22

    Frank Puck Guest

    "Octon22" <> wrote in message
    news:...
    > 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!
     
    Frank Puck, Jan 19, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    745
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    876
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,332
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    658
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    502
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page