0

I have to class that use each other but whatever I tried I couldn't achieve to make them work. I just want them to access each other and after days of struggle, I decided to ask here. If someone can point out what I am doing wrong and what I should do instead it would be great.

Edit: I decided to implement one of the solutions which are already on stack overflow and changed my code according to that: I will also share errors this time so maybe we can figure out what's wrong.

I tried to copy this: Resolve build errors due to circular dependency amongst classes

I used class name "Unit" instead of "A" and "Skill" instead of "B"

Skill.h

#pragma once
#include <iostream>
#include <vector>
#include "Unit.h"
using namespace std;



class Skill :
    public Unit
{

    double _val;
    Unit* unitPtr;
public:

    Skill(double val):_val(val)
    {

    }

    void SetSkill(Unit* unit)
    {
        unitPtr = unit;
        unitPtr->Print();
    }

    void Print()
    {
        cout << "Type:B val=" << _val << endl;
    }



//  Unit* unitPtr;
    vector <Skill *> attacks;
    vector <Skill *> utilities;
    vector <Skill *> movement;


};

Unit.h

#pragma once
#include <iostream>
#include <vector>
#include <stdlib.h> 
#include <time.h> 
using namespace std;

class Skill;
class Unit
{

    int _val;
    Skill* skillPtr;


public:

    Unit(int val) :_val(val)
    {
        stunned = false;
        curSpeed = speed + rand() % 8 + 1;
    }

    void SetSkill(Skill* skill)
    {
        skillPtr = skill;
        skillPtr->Print(); // COMPILER ERROR: C2027: use of undefined type 'B'
    }

    void Print()
    {
        cout << "Type:A val=" << _val << endl;
    }

    int GetDodge()
    {       
        return dodge;
    }   
    void Setup();

    string name;
    int maxHP;
    //... and other variables

};

Unit.cmp

#include "Skill.h"
#include "Unit.h"

void Unit::Setup()
{

    heroes.push_back(new Vestal);
    heroes.push_back(new Vestal);
    heroes.push_back(new Crusader);
    heroes.push_back(new Crusader);

    monsters.push_back(new BoneSoldier);
    monsters.push_back(new BoneDefender);
    monsters.push_back(new BoneSoldier);   
    monsters.push_back(new BoneDefender);

}

Later on the code, I add some stuff to attacks, utilities, and moment and I want to access them from the Unit object, like below:

heroes[0]->skillPtr->attacks[0]

And I want to be able to access the variables in the Unit.h (like maxHP) from Skill.h

Errors:

Error C2512 'Skill': no appropriate default constructor available
Error C2512 'Unit': no appropriate default constructor available

Someone
  • 15
  • 5
  • There are so many posts related to circular dependency. I hope one of them has the answer for you. https://stackoverflow.com/search?q=%5Bcpp%5D+circular+dependency – R Sahu May 12 '20 at 17:08
  • 1
    Does this answer your question? [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) –  May 12 '20 at 17:11
  • Yes there are so many related posts and I tried to apply them but for some reason whatever I try I encountered a different error. And I just gave up after some point – Someone May 12 '20 at 17:34
  • You implemented a solution in skill.h, just use the same pattern in unit.h. – Werner Henze May 12 '20 at 17:47
  • @WernerHenze I applied it but it still gives me the error of: Error C2504 'Unit': base class undefined – Someone May 12 '20 at 18:20
  • For a pointer you just need a forward declaration and no include. For a base class you need the complete declaration, so you need an include. – Werner Henze May 12 '20 at 18:22

1 Answers1

3

I'm a beginner but maybe I can see a couple of issues that are likely causing you problems here.

First you've got a classic circular dependency here, Unit.h includes Skill.h which includes Unit.h which includes Skill.h and so on.

Secondly not seeing any #pragma once pre-proccessor directives. Which means when you then go on to try and include both Skill.h and Unit.h in your unit CPP file, you will try to include the .h files more than once, as they include each other...

Thirdly you're using namespace std in the global scope. You can do that, but don't do that.

If you're looking for more try this excellent video on the subject

https://www.youtube.com/watch?v=Zbw58vTotok

  • Circular dependency one was a mistake I made while I was updating the post, it must be fine now. About the other things, I will look into them thanks. – Someone May 12 '20 at 18:27
  • Btw it was a very nice video and helped me, thank you again. – Someone May 12 '20 at 18:50