0

I was doing some tests with C++, and I came across this scenario.

I have a class A that has a vector (bVec), this vector contains pointers to objects named B.

Class B has a constructor that requires a pointer to an object of type A and an integer value, basically the pointer to the object of type A is used to call its 'put' function.

And finally, class A has a function (draw()) to print all integer values of objects of type B collected in the vector of pointers to objects B, bVec.

In case you are wondering, bVec is a vector of pointers to objects B, because in this way I can modify the integer value of B and consequently update it in the bVec vector.

I know it's a bit confusing, but I'll show you the code now:

A.h:

#pragma once

#include <iostream>
#include <vector>
#include "B.h"

using namespace std;

class A
{
    public:
        void put(B* b);
        void print();

    private:
        vector <B*> bVec;
};

A.cpp:

#include "A.h"

void A::put(B* b)
{
    A::bVec.push_back(b);
}

void A::print()
{
    for (int i = 0; i < A::bVec.size(); i++)
        cout << A::bVec.at(i)->value;
}

B.h:

#pragma once

#include "A.h"

class B
{
    public:
        B(A* a, int value);

        A* a;
        int value;
};

B.cpp:

#include "B.h"

B::B(A* a, int value)
{
    B::a = a;
    B::value = value;

    B::a->put(this);
}

Main.cpp:

#include <iostream>
#include "A.h"

int main()
{
    A a;
    B b(&a, 5);

    b.value = 10;

    a.print();

    // Must print 10;
}

This does not work, the compiler generates a series of errors for me, which I report below:

Severity Code Description Project File Line Deletion Status
Error C2660 'A::put': function does not accept 1 argument AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.cpp 8    
Error C2065 'B': Unreported identifier AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\AreaTest\A.h 16   
Error C2661 'B::B': No function in overload accepts 2 arguments AreaTest C:\Usersers\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\AreaTest\A.cpp 7  
Error C2143 Syntax error: ';' missing before '*' AreaTest C:\Users\albya\Personal\Programming\C++\Software Challenge - 2022\AreaTest\B.h 10 
Error C2143 Syntax error: ';' missing before '*' AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 10  
Error C2059 Syntax error: '>' AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\AreaTest\A.h 16    
Error C2061 syntax error: identifier 'A' AreaTest C:\Users\Albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 8   
Error C2061 syntax error: identifier 'A' AreaTest C:\Users\Albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 8   
Error C2061 syntax error: identifier 'B' AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\AreaTest\A.h 12 
Error C4430 missing type identifier, int will be used. Note: default-int is no longer supported in C++ AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 10    
Error C4430 missing type identifier, int will be used. Note: default-int is no longer supported in C++ AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 10    
Error C2238 unexpected tokens before ';' AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 10  
Error C2238 unexpected tokens before ';' AreaTest C:\Users\albya\Personal\Programming\C++Software Challenge - 2022\AreaTest\B.h 10  

I realised that the error is due to a circular dependency between the classes, but I cannot solve it.

In this post I ask if someone can tell me where I am going wrong (perhaps with some example code) and if there is a better methodology to achieve what I want.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Titan
  • 99
  • 7
  • 1
    No reason for **A.h** to `#include "B.h"`; instead use `struct A;` forward declaration (or put that in a **A.hh** header-header file, and include that instead). Likewise, no reason for **B.h** to `#include "A.h"`. – Eljay Jul 14 '22 at 13:15
  • @Eljay, can you explain with code?, I cannot understand, then I would like to know if you know a better method of doing this **thank you in advance** – Titan Jul 14 '22 at 13:20
  • In file **A.h**, delete the line `#include "B.h"`. Add the line `struct A;`. In **B.h**, delete the line `#include "A.h"`. Add the line `struct B;`. Those are *forward declarations*. In **A.cpp**, you'll need to add `#include "B.h"` (it should have been there to begin with, but it wasn't.) In **B.cpp**, add `#include "A.h"`. – Eljay Jul 14 '22 at 13:28
  • @Eljay, Thank you, now I understand, **another thing there is a better method to achieve this.** – Titan Jul 14 '22 at 13:30
  • I think using a header-header file is a better method to achieve the same result. But that's a matter of opinion, and guidance may vary from team to team. – Eljay Jul 14 '22 at 13:32

0 Answers0