我有一個C++程序,當我嘗試編譯它,它提供了一個錯誤:C++ - 錯誤:之前預期不合格的ID「使用」
calor.h|6|error: expected unqualified-id before ‘using’|
這裏是爲calor
類的頭文件:
#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
爲什麼會發生這種情況?
這個類gradiente
繼承:
#ifndef _GRADIENTE_
#define _GRADIENTE_
#include "sensor.h"
using namespace std;
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
}
#endif
這反過來又來自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
繼承爲了完整起見,這裏的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
我在做什麼錯?
謝謝您補充更正! – 2010-06-03 12:14:57
只是爲了澄清,你永遠不應該使用「using namespace std」(或者任何其他命名空間)的原因是因爲你的類的用戶不會期望他們的全局命名空間受到所有這些東西的污染命名空間std當他們沒有要求它。通常情況下,您希望將添加到頭文件中全局名稱空間的東西限制到完成工作所需的最小值,否則可能會引入名稱衝突和其他有趣的東西。 – George 2010-06-03 12:27:34