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.
Related
In my previous question, I asked that how can I extern classes in a class, and we could do:
namespace kc
{
using ::A;
using ::B;
}
instead of:
namespace kc
{
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
}
And now, I want to do something like this for namespaces, without defining the entire namespace again. I tried to use the same code, but it doesn't work: error: using-declaration may not name namespace <namespace_name>
Edit
Actually, I'm making a kernel, and that's my code (pio.hpp):
#pragma once
namespace pio
{
void outb(unsigned short port, unsigned char value);
void outw(unsigned short port, unsigned short value);
unsigned char inb(unsigned short port);
unsigned short inw(unsigned short port);
}
And (k.hpp):
#pragma once
#include <pio.hpp>
#include <cursor.hpp>
namespace k
{
using ::pio;
using ::cursor;
}
I think what you’re looking for is a namespace alias:
namespace A {
namespace B {
struct C {};
}
}
namespace X {
namespace Y = ::A::B;
}
This would allow you to write e.g.
X::Y::C foo;
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)
I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ‘)’ before ‘n’
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.
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 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.