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
Related
I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.
And I get this type of error :
include\Card.h|6|error: expected class-name before '{' token|
BANCNOTE.H
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>
#include "Card.h"
using namespace std;
class Bancnote
{
public:
Bancnote();
Bancnote(string, int ,int ,int );
~Bancnote( );
int getsumacash( );
void setsumacash( int );
int getsumaplata( );
void setsumaplata( int );
int getrest( );
void setrest( int );
string getnume( );
void setnume( string );
void ToString();
protected:
private:
string nume;
int sumacash;
int rest;
static int sumaplata;
};
#endif // BANCNOTE_H
BANCNOTE.CPP
#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;
Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
this->nume=_nume;
this->sumacash=_sumacash;
this->rest=_rest;
this->sumaplata=_sumaplata;
}
Bancnote::Bancnote()
{
this->nume="";
this->sumacash=0;
this->rest=0;
this->sumaplata=0;
}
Bancnote::~Bancnote()
{
cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}
string Bancnote::getnume()
{
return nume;
}
void Bancnote::setnume(string _nume)
{
this->nume=_nume;
}
int Bancnote::getsumacash()
{
return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
return rest;
}
void Bancnote::setrest(int _rest)
{
this->rest=_rest;
}
void Bancnote::ToString()
{
cout<< "-----"<<getnume()<< "-----"<<endl;
cout<<"Suma Cash: "<<this->getsumacash()<<endl;
cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
cout<<"Restul:"<<this->getrest()<<endl;
}
CARD.H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
class Card: public Bancnote
{
public:
Card();
virtual ~Card();
protected:
private:
};
#endif // CARD_H
You have messed up the includes. What you have is more or less this:
Bancnote.h:
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h" // remove this
struct Bancnote {};
#endif
Card.h
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif
When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.
PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)
#ifndef _ALLOCATOR_H
#define _ALLOCATOR_H
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/MMAP_Memory_Pool.h"
#include "ace/Malloc_T.h"
#include "ace/Null_Mutex.h"
#include "ace/PI_Malloc.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Thread_Mutex.h"
#include "ace/Process_Mutex.h"
#include <string>
using namespace std;
class CAllocator
{
public:
CAllocator();
~CAllocator(void);
public:
bool Create(char* strPoolName);
void Destroy();
public:
char* NewMem(char* strBlockName,int nBlockSize);
char* FindMem(char* strBlockName);
bool FreeMem(char* strBlockName);
private:
typedef ACE_Malloc_T <ACE_MMAP_MEMORY_POOL,
ACE_Process_Mutex,
ACE_PI_Control_Block>
ALLOCATOR;
ALLOCATOR* m_pAllocator;
};
#endif //_ALLOCATOR_H
#ifndef _ARRAY_H
#define _ARRAY_H
#include "allocator.h"
template<typename T>
class CArray
{
public:
bool CreateArray(CAllocator* pAllocator,char* strArrayName,int nArraySize);
bool OpenArray(CAllocator* pAllocator,char* strArrayName);
public:
CArray()
{
m_pArrayData = NULL;
}
~CArray()
{
m_pArrayData = NULL;
}
public:
T* GetObject(int nIndex);
int GetArraySize();
private:
T* m_pArrayData;
};
#include "array.cpp"
#endif //_ARRAY_H
In the function CreateArray of the template class CArray,
the gcc compiler says CAllocator has not been declared.
but all the code worked under vs2010
please help,thanks gurus
Please stop naming like _ALLOCATOR_H. Name start with __ or _ followed by a capital letter is reserved for using by compiler and standard.
– Danh
change #ifndef _ALLOCATOR_H #define _ALLOCATOR_H to #ifndef ALLOCATOR_H #define ALLOCATOR_H Everything is ok! Thank u all – Jack
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
I've looked this up and the closest thing I found was this except I don't have any forward declarations. I only have one pure virtual function in the base class which I'm implementing in the subclass as follows:
Command.h
#ifndef _COMMAND_H_
#define _COMMAND_H_
#include <string>
#include "Stack.h"
#include "Number.h"
class Command
{
public:
std::string cmdType;
Command(void);
Command (std::string cmdType);
virtual void executeCommand(Stack<Number> & stack) = 0;
~Command (void);
};
#endif // !defined _COMMAND_H_
Command.cpp
Command::Command(void)
:cmdType("")
{}
Command::Command(std::string cmdType)
:cmdType(cmdType)
{}
Command::~Command(void)
{}
Number.h
#ifndef _NUMBER_H_
#define _NUMBER_H_
#include "Command.h"
#include "Stack.h"
class Number : public Command
{
public:
Number (float num);
void executeCommand(Stack<Number> & stack);
float val;
~Number (void);
};
#endif // !defined _NUMBER_H_
Number.cpp
#include "Number.h"
Number::Number(float num)
:val(num)
{
cmdType = "hi";
}
void Number::executeCommand(Stack<Number> & stack)
{
stack.push((*this));
}
File error occurs:
Error 4 error C2259: 'Number' : cannot instantiate abstract class c:\...\add.cpp 34
Add.cpp
#include "Add.h"
Add::Add(void)
:Binary("+")
{
}
Add::~Add(void)
{
}
void Add::executeCommand(Stack<Number> & numStack)
{
Number num1 = numStack.top(); //THIS LINE HAS THE ERROR
numStack.pop();
Number num2 = numStack.top();
numStack.pop();
float tempVal = num2.val + num1.val;
num1.val = tempVal;
numStack.push(num1);
}
Add.h
#ifndef _ADD_H_
#define _ADD_H_
#include "Stack.h"
#include "Number.h"
#include "Binary.h"
class Add : public Binary
{
public:
Add (void);
void executeCommand (Stack<Number> & numStack);
~Add (void);
};
#endif // !defined _ADD_H_
This is a circular dependency problem.
Command.h includes Number.h
Number.h includes Command.h
Usually it is solved by replacing one of the includes with a forward declaration, try forward-declaring Number in Command.h instead of including Number.h; move that include to Command.cpp.
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.