Class with pointers to other classes as member data.

This is a discussion on Class with pointers to other classes as member data. within the C++ Programming forums, part of the General Programming Boards category; I am writing a game where a certain location will point to a creature which resides there and also the ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    Class with pointers to other classes as member data.

    I am writing a game where a certain location will point to a creature which resides there and also the creature points to the location where it is located. I am running into problems however because when I declare the location class and add as one of its members a pointer to an as of yet undeclared class creature it gives me an error. Is there any way to make this work? I've rewritten the relevant code and posted it here.

    So the problem basically is that I'm trying to create a pointer to a class that doesn't exist yet.
    Anyone have any ideas?


    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    class location
    {
    public:
    int x;
    int y;
    creature *pCreature; // CLASS CREATURE IS AS OF YET UNDECLARED! //
    }newLocation;
    
    class creature
    {
    public:
    int health;
    int strength;
    location *pLocation;
    }dog;
    
    
    int main()
    {
    dog.pLocation = &newLocation;
    newLocation.pCreature = &dog;
    
    system("PAUSE");
    return 0;
    }
    Gives the error message:

    Code:
    12 C:\Dev-Cpp\newTemp.cpp ISO C++ forbids declaration of `creature' with no type
    12 C:\Dev-Cpp\newTemp.cpp expected `;' before '*' token

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 12
    Last Post: 06-03-2005, 01:13 AM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21