Thread: Problems using list, OOP

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    Problems using list, OOP

    Hi, this is a very long post and im really sorry, but i dont know where else to turn

    I have a main program that has a list of objects (each one a class NPC),
    this is to represent a list of bots, using the STL <list>.

    Im passing that list as a parametre to the NPC class constructor along with an int reference so the NPC knows which NPC in the list he is. The NPC's constructor looks like :

    NPC(list<NPC> *NPClist, int ref);

    First question is am i able to do the above? make class NPC accept as a construtor parametre a pointer to a list of NPC's from back in the main class?

    The reason i need it like this is that NPC has two pointers to other classes as well (Environment and AI) which are 'newed' inside the NPC's constructor for each bot. These objects are inturn passed an NPC pointer of 'this' NPC so that they can refer back to its public functions to change conditions and movement for each tic. The Environment class will be sent the list of NPC's each tic (to refresh it and remove dead bots and update thier status etc) so the AI class can query where the player character and others are. The AI class contains a state machine with 4 states (patrol, chase etc) and calculation functions (finding waypoints, turning, firing etc) and sets the bool states and movement by calling back the NPC public set functions.

    After the initial constuction, i was just planning to cycle through the NPC list in the main program each tic, call reCalc for each one and send it the updated list.

    But i am getting errors like the following:

    environment.h(21) : error C2629: unexpected 'class Environment ('
    environment.h(21) : error C2238: unexpected token(s) preceding ';'
    environment.h(27) : error C2061: syntax error : identifier 'list'
    environment.h(38) : error C2143: syntax error : missing ';' before '<'
    environment.h(38) : error C2501: 'list' : missing storage-class or type specifiers
    environment.h(38) : error C2059: syntax error : '<'
    environment.h(38) : error C2238: unexpected token(s) preceding ';'
    environment.h(41) : error C2143: syntax error : missing ';' before '*'
    environment.h(41) : error C2501: 'NPC' : missing storage-class or type specifiers
    environment.h(41) : error C2501: 'body' : missing storage-class or type specifiers

    ai.h(25) : error C2629: unexpected 'class AI ('
    ai.h(25) : error C2238: unexpected token(s) preceding ';'

    etc...

    those two classes accept the 'this' parametre like so:

    AI(NPC *parent);
    Environment(NPC *parent);

    I really dont know what i am doing wrong and my head cant really think at the moment, i would love some advice if anyone can help pleaseee.

    The two pointers in the NPC header are:

    Environment *Geometry;
    AI *Brain;

    and basically the code in the NPC implimentation file looks like this:

    Code:
    NPC::NPC(list<NPC> *NPClist, int ref) {
         
         //If '0' it means this is the player, who is always the '0' element
         if(ref == 0) {       
             this->is_Player = true;
             this->Brain     = NULL;    
             this->Geometry  = NULL;
         }        
         else
             this->is_Player = false;
         
         this->is_Hurt             = false;
         this->is_Still            = false;
         this->in_Combat           = false;
         this->is_Fleeing          = false;
         this->is_Firing           = false;
         this->is_Meelee           = false;
         this->is_Walking          = true;
         this->is_Running          = false;
         this->is_Dead             = false;
         this->in_Shoot_Range      = false;
         this->in_Meelee_Range     = false;
         this->has_Ammo            = true;
         this->Enemy_Spotted       = false;
         this->Player_Hit	      = false;
         
         this->status              = PATROL;
    
         this->health	      = 100;
         
         if(!is_Player) {
         
             //Creating brain and environment
             try {
                 this->Brain    = new AI(this);
                 this->Geometry = new Environment(this);
             }
             catch(...) {cerr << "\nError, Could not new memory for NPC" << endl;}
             
             //Request think cycle
             reCalc(NPClist);
         }
    }
    
    void NPC::reCalc(list<NPC> *NPClist) {
    	
         //Refresh the NPC surroundings knowledge
         this->Geometry.refresh(NPClist);   
         
         //If this isnt the player, then think and move
         if(!is_Player) {
                        
             Brain.Cycle();
             set_Movement();
         }
    }
    
    void NPC::set_Movement() {
         
         //use updated x,y,z,yaw,pitch position stored in struct motion
         //If not ok, recall brain cycle and try again
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> am i able to do the above? make class NPC accept as a construtor parametre a pointer to a list of NPC's from back in the main class?
    Yes.

    Your compiler errors look like they are due to a lack of #includes. Did you #include <list> in that source file? Did you #include the file that declares the Environment class? The list class is in the std namespace. If you don't want to call it std::list, you have to put a using directive or declaration in the source file, did you do that?

    Is a list the best option for this? If you have an int ref to refer to an item in the list, is that an index or some sort of an ID. Either way that is a very inefficient way of doing a lookup. If you will be looking up these things in the list often, you shouldn't be using a list. A vector is better if ref is an index, and a set or map might be better if ref refers to a key of some sort.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    Yeah i do include the corrent headers:

    Code:
    #include <iostream>
    #include <list>
    #include "Point.h"
    #include "AI.h"
    #include "Environment.h"
    
    //Primary states
    #define PATROL      0 //default
    #define CHASE       1
    #define FIGHT       2
    #define EVADE       3
    
    using namespace std;
    
    class NPC {
    This is really the first time i am using lists as well... so i dont know a great deal of how they operate so to say, and yes the int parametre references to which NPC in the list it is (this is only because the player character will be in the list too, in element 0, so this int is just for the first check to see if its the player), and its only sent when its constucted.

    If i was to use a vector to hold this, would i need to change much code? and is say iterating through the vector handled in a similar way to a list?

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    A vector when reading/modifying data that is already in it, acts just like an array vectorOfStuff[0] would grab the first element.

    You would have to use .push_back(stuff) to put stuff into your vector, but that is easy enough to get used to.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the errors are in ai and environment h so post those for us to try and debug

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    ok ill post the header files, because im still quite new at c++ and i know im making mistakes...

    The Environment header:

    Code:
    #ifndef H_ENVIRONMENT_H
    #define H_ENVIRONMENT_H
    
    #include <iostream>
    #include <vector>
    #include <cmath>
    #include "AI.h"
    #include "NPC.h"
    
    const MIN_DIS = 16.0f;
    
    class Environment {
          
    public:
         
    	Environment(NPC *parent) {this->body = parent;}
    	Environment(const Environment &anotherEnv);
    	const Environment &operator = (const Environment &anotherEnv);
        ~Environment() {}
           
    	//Refresh the NPC knowledge
        void refresh(vector<NPC> *NPClist);
    
    	//Check surroundings for enemy in range
    	bool find_enemy(Point pos);
    
    	//If target found return his position
    	void enemy_position(Point &pos);
    	   
    private:
           
    	//Vector of NPC's
        vector<NPC> Botlist;
    
    	//NPC pointer reference to the parent class
    	NPC *body;
    };
    
    #endif
    The refresh function needs to pretty much store the list that is sent to it, in the Botlist vector, so that it can iterate through it etc.

    The AI header:

    Code:
    #ifndef H_AI_H
    #define H_AI_H
    
    #include <iostream>
    #include <cmath>
    #include <queue>
    #include "NPC.h"
    #include "Pathloader.h"
    #include "Environment.h"
    
    #define PI			3.14159265
    #define FIRE_DIS	8.0
    #define MEELEE_DIS	1.0
    #define BULLETS		25
    
    class AI {
          
    public:
          
    	   AI(NPC *parent);
    	   AI(const AI &anotherAI);
    	   const AI &operator = (const AI &anotherAI);
           ~AI() {}
           
           //Think cycle for this tick
           void Cycle();
    	   
    private:
            
           //Input
           void Sensor();               //Is the player nearby, Calls Environment (Geometry) which is public in NPC, and get input                            
           
           //Path and Memory
           void Memory();               //What rule was i following?
                                        //What happened recently?
                                        //Was i chasing the player?
                                        //Was i firing my weapon?
                                        //Was i in Meelee combat?                             
                                        //What is my current status?
                                 
                                        //Set rule triggers in class 'NPC'
                                 
           //Result (switch)
           void Action();               //Use Sensor and Memory data
                                        //Enable a primary state
            
           //Primary state functions    //Directly affects class 'NPC' and movement
           void Patrol();
           void Chase(); 
           void Fight();
           void Evade();
    
           //Processing and calculation functions
    	   float Get_Target_Range();
           Point Next_Waypoint();
    	   Point Previous_Waypoint();
           Point Closest_Waypoint(Point pos);
           float Get_Position_Angle(Point pos);
           
           //Fight subset functions
           void Fire();
           void Meelee();
    
    	   //Reset state conditions to thier default values
    	   void Default_States();
    
    	   //How many shots fired by this NPC
    	   int entity_count;
           
           //vector containing the waypoints
    	   vector<Point> Waypoints;
    	   
    	   //Queue that contains the memory of past states executed
    	   queue<int> recall;
     
    	   //Loader to gather the raw waypoints
    	   Pathloader loader;
    
    	   //Holds the reference to the parent class
    	   NPC *body;
    };
    
    #endif

    They all compile at the moment... but only because i've commented out the #includes for Environment.h and AI.h in the NPC.h file.

    If i uncomment them i get the same:

    error C2629: unexpected 'class Environment ('
    etc...

    errors

    The 'NPC *parent' in the constructors is the parametre passed to it when it was constructed in NPC using:

    this->Geometry = new Environment(this);

    This was so i can use this pointer to access the get/set functions back inside the NPC class. I really dont know if i am looking at this problem in a very wierd or error prone way, but i would really like to try and figure it out.

    Thanks for the replies so far.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like you have circular includes.
    You have to use forward declarations .
    Kurt
    EDIT: actually you are not using Environment in "ai.h" so you could just remove the #include "Environment.h" in ai.h
    Last edited by ZuK; 10-25-2006 at 01:26 PM.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    you mean like putting 'class AI;' 'class NPC; 'class Environment;' etc?

    where would i put them?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    See the edit of my prev. post. Forward declarations might not be needed.
    Kurt

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    Unhappy

    Even without those header files i still get similar errors, i tried forward declarations, class AI; class Environment; etc, but when i go to 'new' the pointers in the NPC constructor i still get errors like:

    error C2514: 'AI' : class has no constructors

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Those forward declarations work in the headers only. In the implementation files you still have to include the header of each class you want to use.
    Kurt

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    28
    Ah ok, its compiling ok now at least, with those forward declarations telling it to hold off on spamming errors at me until its done lol.

    Im sure im going to run into other complications but at least i've got a better understanding of some things.

    Sorry about the million questions i asked, thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  3. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM