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.