CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Threaded View

  1. #1
    Join Date
    May 2009

    Including a class within another class

    Hi All,

    I'm still very new to C++ so apologies in advance if I am not too clear in my description of my problem.

    I am making a text based drug dealing game which has an Class called Enemy which contains all the information you might expect to find about an enemy (health and weapon stats). This is defined in a .h and .cpp file within my project. I am then making a class called Location which contains information on how many customers want drugs of each type, the maximum number of customers for each drug type and so on. At each location I want to have an array of enemies so that the player must fight off a group of enemies before they can start to deal on that turf.

    So I have my Enemy class working properly and then within the Location class I want to add something like this:

    Enemy enemyGang[5];

    So I have an array of five enemies which I can then tweak each turn based on some simple randomisation so the gang is not always identical.

    When I compile this within Code::Blocks I get the following error.

    `Enemy' does not name a type

    I have written #include "Enemy.h" at the top of the Location.h file so it should know what an Enemy class is. What am I doing wrong?


    #include "Enemy.h"
    class Location
    string name;
    Enemy enemies[5];

    I'd like to make an array of Locations that would be my map and I would access the enemy gang thus.

    locations[0].enemies[0].name = "Gangster1";

    Am I doing something fundamentally wrong? Can you not include a predefined class within another class?

    Any help would come as a great relief!

    Thanking you in advance.

    Last edited by Diamo; May 23rd, 2009 at 01:18 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.