0

I have the following 3 classes: A, B and C. I got errors for circular dependency for random_int(), how can I resolve the following circular dependency?

3x Error: function random_int() is already defined in main.cpp. Files: C.cpp, A.cpp and B.cpp

A.hpp

#include "B.hpp"

int random_int() {

class C
class A {
    public: 
        void set_b(B& be) {b = be}
        B* get_b() {return b;}

        static A& getInstance()
        {
            static A instance;
            return instance;
        }
    private:
        B* b;
        test();
}

A.cpp

#include "A.hpp"
#include "C.hpp"

void test() {
    if (dynamic_cast<C*>(obj)) {
        //do stuff
    }
}

B.hpp

class C;
class B {
    public:
        std::vector<C*> nearby_cs*(C& obj);  
}

B.cpp

#include "B.hpp"
#include "C.hpp"

std::vector<C*> B::nearby_cs*(C& obj) {
//do stuff
}

C.hpp

class A
class C {
    void stuff();
}

C.cpp

#include "C.hpp"
#include "A.hpp"

void stuff() {
    std::vector<C*> cs = A::getInstance().get_b()->nearby_cs(*this);
}
Marodej85
  • 9
  • 2
  • 1
    And what exactly led you to conclude that the problem here is a "circular dependency" and not, say, attempting to assign a reference to a pointer? – Sam Varshavchik Dec 30 '20 at 20:31
  • 2
    _"I got errors"_. Errors usually describe what the problem is. May we see these errors? – Drew Dormann Dec 30 '20 at 20:34
  • 4
    Are you missing some braces and semicolons there? Plese provide a [mcve] and do not keep the error messages to yourself – Quimby Dec 30 '20 at 20:34
  • AFAIK, `std::vector B::nearby_cs*(C& obj)` is not a valid function declaration. – NathanOliver Dec 30 '20 at 20:42
  • 1
    In `A.hpp` you have 2 typos. Replace `{` with a semicolon and add a semicolon after `class C`. Also there should be a semicolon after `class A` in `C.hpp`. There should be semicolons after classes declarations, as well. – Gary Strivin' Dec 30 '20 at 20:56
  • Does this answer your question? [Function already defined error in C++](https://stackoverflow.com/questions/6964819/function-already-defined-error-in-c) – Gary Strivin' Dec 30 '20 at 21:16
  • 1
    This seems to be a case of **writing too much code before testing**. Just including A.hpp will result in several typo-related errors. The existence of other headers doesn't matter. – Drew Dormann Dec 30 '20 at 21:59
  • @DrewDormann Exactly. To the OP: In cases like this, comment out a lot of the code (around 50%) and try again. If the error persists, it's in the remaining code; if not, in the commented out part. Divide the respective part again etc. – Peter - Reinstate Monica Dec 30 '20 at 22:47

2 Answers2

0

Change this

int random_int() {

class C

to this

int random_int();

class C;

I presume the { and missing ; are simply typos.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

Problem:

3x Error: function random_int() is already defined in main.cpp. Files: C.cpp, A.cpp and B.cpp is not a problem caused because of a circular dependency, it is caused because you're defining the random_int() three times. It doesn't have anything to do with the classes.

Solution:

From this question, the possible solutions for your problem are:

  1. Declare the function as static.
  • A.hpp
   static int random int()
   {
      ...
   }
  1. Declare the function as inline.
  • A.hpp
   inline int random int()
   {
      ...
   }
  1. Declare the function in the header (.hpp) file and define it in a source (.cpp) file.
  • A.hpp
   ...
   int random_int();
   ...
  • A.cpp
   ...
   int random_int(){
      ...
   }
   ...

Additional information:

  1. You have multiple typos with semicolons and braces.
Gary Strivin'
  • 908
  • 7
  • 20