5

So I am getting the following errors:

..\Actor.h:35: error: `Attack' is not a member of `RadiantFlux'
..\Actor.h:35: error: template argument 1 is invalid
..\Actor.h:35: error: template argument 2 is invalid
..\Actor.h:35: error: ISO C++ forbids declaration of `attacks' with no type

On this line (among others):

std::vector<RadiantFlux::Attack> attacks;

Here are the relevant files:

Actor.h:

#ifndef ACTOR_H_
#define ACTOR_H_

#include <string>
#include <vector>
#include "Attack.h"

namespace RadiantFlux {

...

class Actor {
private:
    std::string name;
    int health;
    std::vector<RadiantFlux::Attack> attacks;
    Attributes attributes;

public:
    ...
};

}

#endif /* ACTOR_H_ */

Attack.h:

#ifndef ATTACK_H_
#define ATTACK_H_

#include <string>
#include <stdlib.h>
#include <time.h>
#include "Actor.h"

namespace RadiantFlux {

... 

class Attack {
private:
    ...

public:
    ...
};

}

#endif /* ATTACK_H_ */

Why am I getting these errors and what can I do to fix them? I am assuming it has something to do with the namespaces...

cactusbin
  • 671
  • 1
  • 6
  • 11

2 Answers2

12

You have a cyclic dependency of your header files.
Attack.h includes Actor.h and vice versa.
Use Forward Declaration of class to avoid circular dependency problems.


Since the OP's comments, here is what needs to be done:

class Actor;

class Attack
{

};

If your code fails to compile after doing this, You need to read the linked answer and Understand why the error and how to solve it. The linked answer explains it all.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • If I do this I get an error: "..\Actor.h:26: error: forward declaration of `struct RadiantFlux::Attack'" – cactusbin Nov 15 '11 at 16:11
  • I don't think a template parameter can be a forward declaration can it? I think Actor should be forward declared in Attack instead. But truely, I think there's probably a different way to design so that the dependencies are linear rather than cyclic. – Kevin Nov 15 '11 at 16:12
  • @cactusbin: You need to forward-declare `Actor` for `Attack`, not vice versa (why do you need this dependency, anyway?). – Cat Plus Plus Nov 15 '11 at 16:13
  • 1
    @Kevin: There's no rule that says incomplete type cannot be a template argument. There is a rule that says incomplete type cannot be a `std::vector` template argument. – Cat Plus Plus Nov 15 '11 at 16:14
  • If I make a forward declaration of Actor in Attack and include Attack in Actor I get this errror: "..\Actor.h:31: error: ISO C++ forbids declaration of `attacks' with no type" – cactusbin Nov 15 '11 at 16:14
  • @CatPlusPlus I have a function in Attack: int calculateDamage(Actor anActor); – cactusbin Nov 15 '11 at 16:16
  • @CatPlusPlus: I guess because its hard to size an array with an incomplete type. – Kevin Nov 15 '11 at 16:16
  • 1
    make Actor a forward declaration and change the signature to "int calculateDamage(const Actor& anActor);" – Kevin Nov 15 '11 at 16:17
  • @Kevin OK but I am still getting my original errors in Actor.h – cactusbin Nov 15 '11 at 16:18
  • @cactusbin: I added an link in the answer for you to read! Unless you read how are you going to understand? The answer linked aptly explains what you can do with forward declared types and what you cannot.Please read and if you still do not understand then comeback and ask your Q's. – Alok Save Nov 15 '11 at 16:19
  • @Als I did read it, and perhaps I am simply misunderstanding but I am not having a problem with my forward declaration of Actor in Attack.h I am having a problem with my included definition of Attack in Actor.h – cactusbin Nov 15 '11 at 16:21
0

The classes Actor and Attack both refer to each other, so you will need to add a forward declaration in one of the file.

For example, in Actor.h:

class Attack;

class Actor
{
    ...
};
Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
codablank1
  • 6,055
  • 5
  • 19
  • 29