Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace problems

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...

like image 458
cactusbin Avatar asked Nov 15 '11 16:11

cactusbin


People also ask

What is namespace issue?

The namespaces in the computing world mandate that uniqueness is ensured. For example, you must have unique names for all the files in a directory or functions that are part of the same program. Namespace collision occurs if parts of the namespace delivered by different people have the same names.

What is a namespace example?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.

What namespace mean?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What are the two types of namespaces?

When creating a namespace, you must choose one of two namespace types: a stand-alone namespace or a domain-based namespace. In addition, if you choose a domain-based namespace, you must choose a namespace mode: Windows 2000 Server mode or Windows Server 2008 mode.


1 Answers

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.

like image 130
Alok Save Avatar answered Nov 07 '22 08:11

Alok Save