0

The question is hard to ask but I'll try to explain: I have a class World that import Class Species, and holds list of Species. I need to have pointer to class World in class Species, but if I import Class World before declaring Species errors occurs.

Example in pseudo code:

import B
Class A
{
list of B
}
Class B
{
Class A* a
}

so I need to import class A to make pointer to it, but when I do Class B is not declared in clas A.

my code:


    
#pragma once
#include <iostream>

class Species
{

protected:
    std::string speciesName;
    int strengh;
    int effort;
    int posX;
    int posY;
    int specieSymbol;

public:
    Species();
    Species(std::string speciesName, int strengh, int effort, int posX, int posY);

    int getPosX();
    int getPosY();

    void setPosX(int x);
    void setPosY(int y);

    int getSpeciesSymbol();

    std::string getSpeciesName();

    virtual void action() = 0;
    virtual void colision() = 0;

    //void setWorld(World* w);
    //World* getWorld();

    ~Species();
};

#pragma once
#include <iostream>
#include "Species.h"
#include <list>
#include "Board.h"
class World
{
private:
    std::list <Species*> species;
    Board* board;

public:
    World(Board *board);

    void addSpecies(Species* s);
    
    void printSpecies();

    void printWorld();

    void Update();
    
    ~World();
};

zywy
  • 100
  • 7
  • 2
    Does [this](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) help answer your question? – Nathan Pierson Apr 09 '21 at 16:24
  • Read (on paper) [a good C++ programming book](https://www.stroustrup.com/programming.html). Take inspiration from *existing* open source projects (like [FLTK](http://fltk.org/), [Qt](http://qt.io), [fish](https://fishshell.com/) and [RefPerSys](http://refpersys.org/)...). Use the [*Clang Static Analyzer*](https://clang-analyzer.llvm.org/) – Basile Starynkevitch Apr 09 '21 at 16:29
  • I think it's what I mean, but I have hard time understanding it, but will keep on trying. – zywy Apr 09 '21 at 16:29
  • Compile your C++ code with [GCC](http://gcc.gnu.org/) invoked as `g++ -Wall -Wextra -g`. For [RefPerSys](http://refpersys.org/) contact me by email to `basile@starynkevitch.net`. See also [this C++ reference](https://en.cppreference.com/w/cpp). Use the [GDB](https://www.gnu.org/software/gdb/) debugger. If allowed, install [Debian](http://debian.org/) on your laptop – Basile Starynkevitch Apr 09 '21 at 16:29

0 Answers0