0

I'm starting a new project and I have trouble with accessing protected methods of Organism inside World class. I suppose there must be some error with my definition of World being a friend of organism. I tried calling some method from Organism inside World, but the compiler says that it is inaccessible. The method was of course set as protected, so only derived classes and friends could call them.

World.h:

 #include <vector>
 #include <iostream>
using std::vector;  

#include <map>
using std::map;
#include "Organism.h"
#pragma once
class World
{
private:
    map<int, std::shared_ptr<Organism>> organims_map;
    vector <std::shared_ptr<Organism>> animals_vector;
    int x_size, y_size; 
    void initiate_aniamals();
public:
    World();
    World(int x, int y);
    void make_turn();

    
};

Organism.h:

#pragma once
#include "World.h"
class Organism
{
    friend class World;
private:
    int strength, vigor;
    int x_pos, y_pos;
    float level;
protected:
    int get_vigor() const;
    virtual void action() = 0 ;
    virtual void collision() = 0;
    /// <summary>
    /// compares animals by their vigor
    /// </summary>
    /// <param name="other organism"></param>
    /// <returns>which animal has higher vigor</returns>
    bool operator<(const Organism& other_organism);
}; 

Then In file world.cpp i try to define method make_turn():

void World::make_turn()
{
    //stable sort animals by their vigor
    std::stable_sort(begin(this->animals_vector), end(this->animals_vector),
        [](const Organism& organism_1, const Organism& organism_2)
        {
            return  organism_1.get_vigor(); //
        });

I get error in line:

 return  organism_1.get_vigor(); 

says that function get_vigor is inacessible.

Michał Turek
  • 701
  • 3
  • 21
  • you should add the actual error message to your question – Alessandro Teruzzi Sep 19 '21 at 12:36
  • Perhaps due to the circular inclusion you have? If you don't include the header files in each other and only have forward declarations of the classes, does it work then? – Some programmer dude Sep 19 '21 at 12:36
  • It looks like you could remove `#include "Organism.h"` from `World.h` and `#include "World.h"` from `Organism.h` and forward declare `Organism` and `World` instead. – Ted Lyngmo Sep 19 '21 at 12:42
  • The `std::shared_ptr` begin passed to your comparison lambda is not an `Organism` for the `const Organism&` parameters. – Eljay Sep 19 '21 at 14:27

1 Answers1

0

The problem was the fact that #pragma once was not at the beginning of World.h But after including Organism.h. That leads to tons of weird errors including the fact that despite being friends of Organism, World couldn't use its private methods.

This is correct:

#pragma once
#include "Organism.h"

This, however is absolutely not:

#include "Organism.h"
#pragma once
Michał Turek
  • 701
  • 3
  • 21
  • `#pragma once` is not standard. For my compiler, it can appear anywhere in the header file and it is effective. Zachary Weinberg, the creator of `#pragma once`, advises [against](https://stackoverflow.com/a/34884735/4641116) using it, and recommends include guards instead which are supported by the C++ standard. – Eljay Sep 19 '21 at 14:37