C++ is not a namespace - c++

I don't really know how to explain this.
I have 2 classes World and Entity in the namespace:
artemis::system;
and one class Component in
artemis::component;
I already had trouble with World and Entity, when trying compile it said either one of them wasn't a declared type. ( they both include each other)
So in both classes, within the the namespace, I've added a forward declaration. This seems to solve the problem.
Now when I try to include "world" in my Component.h it gives me the following error:
component is not a namespace-name
My Component class resides within the namespace artemis::component.
This is driving me crazy. I really don't get what is causing this error.
Header Component.h
#ifndef _COMPONENT_H_
#define _COMPONENT_H_
#include <bitset>
#include "BitSize.h"
#include "World.h"
#include <unordered_map>
#include <typeinfo>
#include <string>
#include <iostream>
#include <assert.h>
//#include "EntityManager.h"
using namespace std;
using namespace artemis::system;
namespace artemis {
namespace component {
class Component {
public:
virtual ~Component() = 0;
protected:
Component(){};
};
/**
* Identifies a bitset and id for a component object
* Do not instantiate a ComponentType, instead use the ComponentTypeManager.
* */
class ComponentType {
public:
ComponentType();
bitset<BITSIZE> getBit() const;
int getId() const;
private:
static bitset<BITSIZE> nextBit;
static int nextId;
bitset<BITSIZE> bit;
int id;
void init();
};
//Surpress unused variable warnning. Might need to rewrite it
//#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored "-Wunused-variable"
/**
* Manages the id and bitset for every component based on their type.
* */
class ComponentTypeManager {
private:
ComponentTypeManager();
static unordered_map<size_t,ComponentType*> componentTypes;
public:
/**
*
**/
static ComponentType & getTypeFor(const type_info &t);
/**
* Gets the component type object
**/
template<typename c>
static ComponentType & getTypeFor() {
//Check if we are being legal with components and shizzle
//Component * c = (component*)0;
assert((std::is_base_of< Component, c >::value == true));
return getTypeFor(typeid(c));
}
/**
* Gets the bit set of a component
**/
template<typename c>
static bitset<BITSIZE> getBit() {
//Check if we are being legal with components and shizzle
//Component * c = (component*)0;
assert((std::is_base_of< Component, c >::value == true));
ComponentType & type = getTypeFor(typeid(c));
return type.getBit();
}
/**
* Gets the component id
**/
template<typename c>
static int getId() {
//Check if we are being legal with components and shizzle
assert((std::is_base_of< Component, c >::value == true));
ComponentType & type = getTypeFor(typeid(c));
return type.getId();
};
//typedef getCompBit bitset<BITSIZE>(*getBit<Component>)();
};
//#pragma GCC diagnostic pop
/*template<typename T>
class ComponentMapper {
private:
//ComponentType * type;
EntityManager * em;
public:
ComponentMapper(World &world) {
em = world.getEntityManager();
//type = ComponentTypeManager::getTypeFor<T>();
}
~ComponentType() {
type = nullptr;
em = nullptr;
}
T & get(Entity * e) {
return &(T)em->getComponent<T>(e);
}
};*/
};
};
#endif
Header Entity.h
#ifndef _ENTITY_H
#define _ENTITY_H
#include <bitset>
#include <string>
#include <cstddef>
#include <typeinfo>
#include "BitSize.h"
#include "Component.h"
#include "ImmutableBag.h"
#include "World.h"
//#include "EntityManager.h"
using namespace artemis::util;
using namespace artemis::component;
//using namespace artemis;
using namespace std;
namespace artemis {
namespace system {
class World;
class Entity {
private:
int id;
long int uniqueId;
bitset<BITSIZE> typeBits;
bitset<BITSIZE> systemBits;
World * world;
//EntityManager * entityManager;
protected:
public:
Entity(World * world, int id);
~Entity();
int getID();
void setUniqueId(long int uniqueId);
long int getUniqueID();
bitset<BITSIZE> getTypeBits();
void addTypeBit(bitset<BITSIZE> bit);
void removeTypeBit(bitset<BITSIZE> bit);
bitset<BITSIZE> getSystemBits();
void addSystemBit(bitset<BITSIZE> bit);
void removeSystemBit(bitset<BITSIZE> bit);
void setSystemBits(bitset<BITSIZE> systemBits);
void setTypeBits(bitset<BITSIZE> typeBits);
void reset();
string toString();
void addComponent(Component * c);
//Might change to non template
template<typename c>
void removeComponent() {
//entityManager->removeComponent(this,ComponentTypeManager::getTypeFor<c>());
}
void removeComponent(ComponentType & type);
bool isActive();
Component * getComponent(ComponentType & type);
/*template<typename c>
Component * getComponent() {
return (c)entityManager->getComponent(ComponentTypeManager.getTypeFor<c>());
}*/
ImmutableBag<Component*> * getComponents();
void refresh();
void remove();
void setGroup(string group);
void setTag(string tag);
};
};
};
#endif // $(Guard token)
Header World.h
#ifndef _WORLD_H_
#define _WORLD_H_
//#include "SystemManager.h"
//#include "EntityManager.h"
#include "ImmutableBag.h"
#include "Entity.h"
using namespace artemis::util;
namespace artemis {
namespace system {
class Entity;
class World {
public:
World();
~World();
//SystemManager * getSystemManager();
//EntityManager * getEntityManager();
//TagManager * getTagManager();
//GroupManager * getGroupManager();
int getDelta();
void setDetla(int delta);
void deleteEntity(Entity *e);
void refreshEntity(Entity *e);
//Entity* createEntity();
//Entity* getEntity(int entityID);
void loopStart();
private:
//SystemManager * systemManager;
//EntityManager * entityManager;
//TagManager * tagManager;
//GroupManager * grouManager;
int delta;
Bag<Entity*> refreshed;
Bag<Entity*> deleted;
};
};
};
#endif // $(Guard token)

Your problem is that you require Component to already be defined in order to create World, and you require World to be defined in order to create Component. This is impossible; each one's definition requires the other.
You need to restructure your classes so that you either don't require Component to be defined in World's header, OR you don't require World to be define in Component's header. You can always include them in the .cpp, but not the header. It's a circular inclusion problem. You could also forward-declare one of the classes.
You are essentially saying: I cannot create A unless I have already created B; I cannot create B unless I have already created A. That's impossible, and the compiler is telling you so.

Related

C++ build - namespace and no member

I have this code:
MyMathUtils.h
#ifndef MY_MATH_UTILS_H
#define MY_MATH_UTILS_H
#include "./Ray.h"
#include "./RayTriangleResult .h"
#include "./Triangle.h"
namespace MyMath
{
class MyMathUtils
{
public:
static bool RayTriangleIntersection(const MyMath::Ray & ray, const MyMath::Triangle & t, MyMath::RayTriangleResult * out);
//other code
};
}
#endif
and
Triangle.h
#ifndef MY_TRIANGLE_H
#define MY_TRIANGLE_H
namespace MyMath
{
struct Triangle
{
//code
};
}
#endif
But during compile, I have got this error:
D:\C++\C++\MyMath\header./MyMathUtils.h(71): error : namespace
"MyMath" has no member "Triangle"
static bool
RayTriangleIntersection(const MyMath::Ray & ray, const
MyMath::Triangle & t, MyMath::RayTriangleResult * out);
I am using Intel C++ 15.0 compiler

How to forward declare a class which is in a namespace

I am trying to use forward declarations in header files to reduce the number of #include used and hence reduce dependencies when users include my header file.
However, I am unable to forward declare where namespaces are used. See example below.
File a.hpp:
#ifndef __A_HPP__
#define __A_HPP__
namespace ns1 {
class a {
public:
a(const char* const msg);
void talk() const;
private:
const char* const msg_;
};
}
#endif //__A_HPP__
File a.cpp:
#include <iostream>
#include "a.hpp"
using namespace ns1;
a::a(const char* const msg) : msg_(msg) {}
void a::talk() const {
std::cout << msg_ << std::endl;
}
File consumer.hpp:
#ifndef __CONSUMER_HPP__
#define __CONSUMER_HPP__
// How can I forward declare a class which uses a namespace
//doing this below results in error C2653: 'ns1' : is not a class or namespace name
// Works with no namespace or if I use using namespace ns1 in header file
// but I am trying to reduce any dependencies in this header file
class ns1::a;
class consumer
{
public:
consumer(const char* const text) : a_(text) {}
void chat() const;
private:
a& a_;
};
#endif // __CONSUMER_HPP__
Implementation file consumer.cpp:
#include "consumer.hpp"
#include "a.hpp"
consumer::consumer(const char* const text) : a_(text) {}
void consumer::chat() const {
a_.talk();
}
Test file main.cpp:
#include "consumer.hpp"
int main() {
consumer c("My message");
c.chat();
return 0;
}
UPDATE:
Here is my very contrived working code using the answer below.
File a.hpp:
#ifndef A_HPP__
#define A_HPP__
#include <string>
namespace ns1 {
class a {
public:
void set_message(const std::string& msg);
void talk() const;
private:
std::string msg_;
};
} //namespace
#endif //A_HPP__
File a.cpp:
#include <iostream>
#include "a.hpp"
void ns1::a::set_message(const std::string& msg) {
msg_ = msg;
}
void ns1::a::talk() const {
std::cout << msg_ << std::endl;
}
File consumer.hpp:
#ifndef CONSUMER_HPP__
#define CONSUMER_HPP__
namespace ns1
{
class a;
}
class consumer
{
public:
consumer(const char* text);
~consumer();
void chat() const;
private:
ns1::a* a_;
};
#endif // CONSUMER_HPP__
File consumer.cpp:
#include "a.hpp"
#include "consumer.hpp"
consumer::consumer(const char* text) {
a_ = new ns1::a;
a_->set_message(text);
}
consumer::~consumer() {
delete a_;
}
void consumer::chat() const {
a_->talk();
}
File main.cpp:
#include "consumer.hpp"
int main() {
consumer c("My message");
c.chat();
return 0;
}
To forward declare class type a in a namespace ns1:
namespace ns1
{
class a;
}
To forward declare a type in multiple level of namespaces:
namespace ns1
{
namespace ns2
{
//....
namespace nsN
{
class a;
}
//....
}
}
Your are using a a member of consumer which means it needs concrete type, your forward declaration won't work for this case.
For nested namespaces, since C++17, you can do
namespace ns1::ns2::nsN
{
class a;
}
Apart to forward-declare the class from within its namespace (as #billz says), remember to either use (prepend) that namespace when referring to the forward-declared class, or add a using clause:
// B.h
namespace Y { class A; } // full declaration of
// class A elsewhere
namespace X {
using Y::A; // <------------- [!]
class B {
A* a; // Y::A
};
}
Ref: Namespaces and Forward Class Declarations

Typeinfo for ocl::CRException

I tried to run my C++ program using g++ but am getting following exception:
"typeinfo for ocl::CRException" am using ocl namespace containing CRException class.. Please, help me on this.
Here is the code where am including OCL classes :
#ifndef VOIDSOFT_ADA2_LIND_HH
#define VOIDSOFT_ADA2_LIND_HH
#include <string>
#include <list>
#include <queue>
#include <map>
#include <ocl.h>
#include "threaded.hh"
using namespace std;
using namespace ocl;
class circuit;
class Lind: public Threaded
{
public:
typedef int lookup_t;
private:
OraConnection connection;
pthread_mutex_t *done_m;
queue<pthread_t> *q;
lookup_t type;
public:
circuit *c;
Lind();
~Lind();
void *run();
map<string, pair<string, string> > *getnodes(string);
bool connect();
void disconnect();
private:
circuit *getcircuitinfo(string, circuit *c = 0);
void bindInStr(OraQuery &q, string arg, string bindvar, map<string, string> *properties);
map<string, pair<string, string> > *node2name(string);
};
/* Lookup types */
namespace LindLookupTypes {
/* Get overlying */
const int OL = 0x001;
/* Get underlying */
const int UL = 0x002;
}
#endif /* VOIDSOFT_ADA2_LIND_HH */
It usually means you forgot to define (implement) a virtual method somewhere. Check that all virtual methods of ocl::CRException are defined and that the object file where they are defined is actually linked to your code.
If it doesn't help, edit the answer and show us the code for CRException.

How do I call a class by passing it's object and member function to another function in c++?

How do I execute a member's function by passing the object and the member's function to another function in c++. I do understand the answer to my question is out there; however, I do not know what this is called. So far I created 2 files, exeFunc.h and exeFunc.cpp. Their code consist of:
exeFunc.h
/*
File: exeFunc.h
Header file for exeFunc Library.
*/
#ifndef EXEFUNC_H
#define EXEFUNC_H
#include "mbed.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include <map>
class exeFunc
{
public:
exeFunc(msExtensions &msExt, cfExtensions &cfExt);
private:
void _splitFuncFromCmd();
void _attachCallback();
msExtensions &_msExt;
cfExtensions &_cfExt;
//FunctionPointer _p;
};
#endif
exeFunc.cpp
/*
File: exeFunc.cpp
Execute functions in other Sensor libraries/classes
Constructor
*/
#include "mbed.h"
#include "ConfigFile.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include "exeFunc.h"
#include <map>
#include <string>
using namespace std;
exeFunc::exeFunc(msExtensions &msExt, cfExtensions &cfExt) : _msExt(msExt), _cfExt(cfExt)
{
//_cfExt.checkConfigForFirstStart();
//_p.attach(&_cfExt, &cfExtensions::checkConfigForFirstStart);
//_p.call();
}
void exeFunc::_splitFuncFromCmd()
{
}
void exeFunc::_attachCallback()
{
}
I wrote a completed example, may helps
class MyClass
{
public:
MyClass(int b)
:_b(b)
{
}
int Foo(int a)
{
return a * _b;
}
int _b;
};
typedef int (MyClass::*MFP)(int);
int get_result(MyClass* obj, MFP mfp)
{
int r = (obj->*mfp)(5); // 30
return r;
}
int _tmain(int argc, _TCHAR* argv[])
{
MFP mfp = &MyClass::Foo;
MyClass m(6);
get_result(&m, mfp);
return 0;
}
You call it by another function.if you have an independent function.
To be honesty your question is not completely clear.However :
int F(int,int,int);
int g();
//main scope
F(g(),a,b)

C++ - Error: expected unqualified-id before ‘using’

I have a C++ program and when I try to compile it it gives an error:
calor.h|6|error: expected unqualified-id before ‘using’|
Here's the header file for the calor class:
#ifndef _CALOR_
#define _CALOR_
#include "gradiente.h"
using namespace std;
class Calor : public Gradiente
{
public:
Calor();
Calor(int a);
~Calor();
int getTemp();
int getMinTemp();
void setTemp(int a);
void setMinTemp(int a);
void mostraSensor();
};
#endif
Why does this happen?
This class inherits from gradiente:
#ifndef _GRADIENTE_
#define _GRADIENTE_
#include "sensor.h"
using namespace std;
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
}
#endif
Which in turn inherits from sensor
#ifndef _SENSOR_
#define _SENSOR_
#include <iostream>
#include <fstream>
#include <string>
#include "definicoes.h"
using namespace std;
class Sensor
{
protected:
int tipo;
int IDsensor;
bool estadoAlerta;
bool estadoActivo;
static int numSensores;
public:
Sensor(/*PARAMETROS*/);
Sensor(ifstream &);
~Sensor();
int getIDsensor();
bool getEstadoAlerta();
bool getEstadoActivo();
void setEstadoAlerta(int a);
void setEstadoActivo(int a);
virtual void guardaSensor(ofstream &);
virtual void mostraSensor();
// FUNÇÃO COMUM
/* virtual int funcaoComum() = 0;
virtual int funcaoComum(){return 0;};*/
};
#endif
For completeness' sake, here's definicoes.h
#ifndef _DEFINICOES_
#define _DEFINICOES_
const unsigned int SENSOR_MOVIMENTO = 0;
const unsigned int SENSOR_SOM = 1;
const unsigned int SENSOR_PRESSAO = 2;
const unsigned int SENSOR_CALOR = 3;
const unsigned int SENSOR_CONTACTO = 4;
const unsigned int MIN_MOVIMENTO = 10;
const unsigned int MIN_SOM = 10;
const unsigned int MIN_PRESSAO = 10;
const unsigned int MIN_CALOR = 35;
#endif
What am I doing wrong?
There is a semicolon missing at the end of this class:
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
} // <-- semicolon needed after the right curly brace.
Also, the names of your include guards are illegal. Names that begin with an underscore and an uppercase letter are reserved for the C++ implementation (as are names containing a double underscore) - you are not allowed to create such names in your own code. And you should never use:
using namespace std;
in a header file. And lastly, the destructor in your Sensor base class should almost certainly be made virtual.
In gradiente.h you forgot the semicolon at the end of your class declaration.
You need this:
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
};
See the added semicolon?
You forgot to leave the last semi colon on the closing brackets, };, on the gradiente class.