abraker95 / tanks

2D arcade top-view shooting game
1 stars 0 forks source link

Interdependency Loop Challange #37

Closed ghost closed 9 years ago

ghost commented 9 years ago

// ****A.H ********

include

include "B.h"

class A
{ public: A(); ~A();

vector<B>\ getB() { return Blist; }

void Update() { for(int i=0; isize(); i++) (*Blist)[i]->Update(); }

private: vector<B>\ Blist; };

// ****B.H ********

include "A.h" // PROBLEM

class B { public: B() { x = 0; } ~B();

int getX() { return x; }

void Update(A* _a) { if(_a->getB()->at(i) != nullptr) x = x-_a->getB()->at(i)->get; else if(_a->getB()->at((_a->getB()->size()-1) != nullptr x = x- _a->getB()->at((_a->getB()->size()-1)->getX(); else x++; }

private: int x; };

ghost commented 9 years ago

That's the challenge I was talking about. Make the above code work with minimal change to its design. To restate the problem above:

ghost commented 9 years ago

The reason you're putting everything in the .h is because those are template classes?

ghost commented 9 years ago

If they are not, the problem is easy to solve:

A.h

// B.h is not included here
class B; // declare B here, just declaration

class A
{
public:
    A();
    ~A();

    vector* getB();
    void Update():

private:
    vector* Blist;
};

A.cpp

#include "B.h" // include B.h
#include "A.h"

vector* A::getB()
{
    return Blist;
}

void A::Update()
{
    for(int i=0; isize(); i++) (*Blist)[i]->Update();
}

B.h

// *************B.H *****************
#include "A.h" // here A.h is included

class B
{
public:
B()
{
    x = 0;
}

~B();

    int getX();
    void Update(A* _a):

private:
    int x;
};

B.cpp

#include "A.h" // we can include both, its just the headers
#include "B.h"

int B::getX()
{
    return x;
}

void B::Update(A* _a)
{
    if(_a->getB()->at(i) != nullptr)
        x = x-_a->getB()->at(i)->get;
    else if(_a->getB()->at((_a->getB()->size()-1) != nullptr
        x = x- _a->getB()->at((_a->getB()->size()-1)->getX();
    else
        x++;
}

I hope you can understand why it works. It's just because the delcaration and the definition are pulled apart.

ghost commented 9 years ago

So the headers are included in the source files. Let me see if I can apply this.

On Fri, Nov 21, 2014 at 8:19 PM, Sherushe notifications@github.com wrote:

If they are not, the problem is easy to solve:

A.h

// B.h is not included hereclass B; // declare B here, just declaration class A {public: A(); ~A();

vector* getB();
void Update():

private: vector* Blist; };

A.cpp

include "B.h" // include B.h

include "A.h"

vector* A::getB() { return Blist; } void A::Update() { for(int i=0; isize(); i++) (*Blist)[i]->Update(); }

B.h

// ****B.H ********

include "A.h" // here A.h is included

class B {public:B() { x = 0; } ~B();

int getX();
void Update(A* _a):

private: int x; };

B.cpp

include "A.h" // we can include both, its just the headers

include "B.h"

int B::getX() { return x; } void B::Update(A* _a) { if(_a->getB()->at(i) != nullptr) x = x-_a->getB()->at(i)->get; else if(_a->getB()->at((_a->getB()->size()-1) != nullptr x = x- _a->getB()->at((_a->getB()->size()-1)->getX(); else x++; }

I hope you can understand why it works. It's just because the delcaration and the definition are pulled apart.

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/37#issuecomment-64062329.

ghost commented 9 years ago

Did it work?

ghost commented 9 years ago

I will check tommorow. I am yet to settle from my trip. On Nov 26, 2014 11:20 AM, "Sherushe" notifications@github.com wrote:

Did it work?

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/37#issuecomment-64670501.