#### Archived

This topic is now archived and is closed to further replies.

# communication between two classes. How?

This topic is 5881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, I don''t understand the following problem: I have three classes (GARDEN ANT and NEST). they all inherits from a class (SPATIALOBJECT). SPATIALOBJECT | | ---------------------------------------- | | | GARDEN NEST ANT furthermore a NEST object has an array of ants a garden has a vector of nests in spatialobject class, there is a function which compares the position of two spatialobject and return an int prototype: int compare(SpatialObject); my goal is to compare the position of each nest with the one of each ant. for each nest for each ant ant->compare(nest) where should I write such a method? In Ant, Nest, Garden class? the most intuitive place would be in ant. But I can''t have access to the nest member from ant. How can I have access? tank you for any help you could give zip7000

##### Share on other sites
Why do you need access to the nest member? Where is this nest member?

something like this?

  class SpatialObject{public:  int compare(const SpacialObject& other);private:  position_t m_position;};class Ant : public SpatialObject{  // ant attribs};class Nest : public SpatialObject{  std::vector<Ant> m_ants;  int compare_ants()  {    int result;    std::vector<Ant>::iterator ant = m_ants.begin();    for(;ant!=m_ants.end();++ant)    {      compare(*ant); // calling Nest::compare(const Ant&);    }    return result;  }};class Garden : public SpatialObject{  std::vector<Nest> m_nests;  int compare_nests()   {    int result;    std::vector<Nest>::iterator nest = m_nests.begin();    for(;nest!=m_nests.end();++nest)    {      compare(*nest); // Calling Garden::compare(const Nest&)    }    return result;  }};

At least, that''s how I understand it :\

##### Share on other sites
Put the compare method in the spatial base class, make sure you have all the info you need there.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]

##### Share on other sites
yes the classes looks like to that.

I tried to get all the info in the ant class. So I create in Ant a method(FindSpatialObject) which takes Garden* object as parameters. If I can have an garden object in the ant class I can access to all the objects(vector of nest, ants..)

bool FindSpatialObject(Garden* g) //find if there is an object at the position of the one of the ant.

But when I include Garden.h I have an error that I don''t understand. It tells me that there is an error to the Nest class(No change has been done in this class)

here the different include in each class:

ANT CLASS
#include "SpatialObject.h"
#include "Garden.h"

NEST CLASS
#include "Ant.h"
#include "SpatialObject.h"

GARDEN CLASS
#include <vector>
#include "SpatialObject.h"
#include "Cell.h"
#include "Nest.h"
#include "Prey.h"
#include "Obstacle.h"

here the error message in NEST class when I write #include "Garden.h" in Ant class

h:\antsim2\nest.h(24) : error C2143: syntax error : missing '';'' before ''*''
h:\antsim2\nest.h(24) : error C2501: ''Ant'' : missing storage-class or type specifiers
h:\antsim2\nest.h(24) : error C2501: ''GetAntColony'' : missing storage-class or type specifiers

here the line which causes the error(NEST class)
Ant* GetAntColony();

is someone knows why?

##### Share on other sites
i can''t be 100% sure because u didn''t post code to the header files, but i have a few hypothesis.

this may sound stupid, but u include-guard''ed all ur header files right (#ifndef _HEADERNAME_H_, #define _HEADERNAME_H_, #endif)?

ok, if that''s not it (i sure hope not! ), then perhaps you made a stupid mistake and forgot a } or a ; somewhere? you know how if you have two lines of code:

printf("this line is missing a semicolon at the end")printf("but the compiler will report an error on THIS line");

what it says in the quotes. so maybe a header above the one that gets the errors (h:\antsim2\nest.h(24)) is all right, but the one above (Cell.h) is missing a } or ;. that''s quite possible, because 24 is pretty much at the top of the file (sort of...).

so please post the code to nest.h and Cell.h, and i will see if there''s anything wrong.

---
shurcool
wwdev

##### Share on other sites
I include-guard''ed all ur header files as you said. but know I have lot of error message.
I give you the code of nest and cell as you requested.
I was wondering if I have to put using namespace std in each file.
I am a beginner in c++ and it can be a stupid mistake. So I will checked any idea you could have.
thank you

// Nest.h: interface for the Nest class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_NEST_H__E908704C_00F2_452E_B239_89F65252C69C__INCLUDED_)
#define AFX_NEST_H__E908704C_00F2_452E_B239_89F65252C69C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef SP_OB
#include "SpatialObject.h"
#define SP_OB
#endif

#ifndef ANT_SYMBOL
#include "Ant.h"
#define ANT_SYMBOL
#endif

#define ANTS_IN_NEST 200
#define WIDTH_NEST 4
#define HEIGHT_NEST 4

class Nest : public SpatialObject
{
public:
int m_idNest;
Ant* GetAntColony();

static int NestCounter; // store the number of nests created.
static int IdNest; // store a unique value

//constructor
Nest(int x, int y);

//destructor
virtual ~Nest();

private:
Ant* m_antsColony[200];
};

#endif // !defined(AFX_NEST_H__E908704C_00F2_452E_B239_89F65252C69C__INCLUDED_)

// Cell.h: interface for the Cell class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CELL_H__714EE797_6C15_4584_92A2_0A46AA422697__INCLUDED_)
#define AFX_CELL_H__714EE797_6C15_4584_92A2_0A46AA422697__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Cell
{
public:
bool GetCellFree();
int GetPheromoneQty();
Cell();
virtual ~Cell();

private:
bool m_cellFree;
int m_pheromoneQty;
};

#endif // !defined(AFX_CELL_H__714EE797_6C15_4584_92A2_0A46AA422697__INCLUDED_)

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007208

×