-1
我想編寫確定性有限自動機並需要兩個類。狀態和轉換,但我想在類中包含兩個轉換類型的對象,但我的品牌狀態不能識別一個轉換。未在此範圍內聲明的類成員
Trans.h:
#ifndef TRANS_H
#define TRANS_H
using namespace std;
#include <string>
class Trans
{
private:
int direccion;
string simbolo;
public:
Trans();
Trans(int dir, string sim);
void setsm(string sm);
void setdir(int dir);
int getdir();
string getsm();
virtual ~Trans();
};
#endif // TRANS_H
Trans.c:
#include "Trans.h"
#include <string.h>
Trans::Trans(int dir,string sim)
{
direccion=dir;
simbolo=sim;
}
Trans::~Trans()
{
//dtor
}
void Trans::setsm(string sm){
simbolo=sm;
}
void Trans::setdir(int dir){
direccion=dir;
}
int Trans::getdir(){
return direccion;
}
string Trans::getsm(){
return simbolo;
}
Estado.h:
#ifndef ESTADO_H
#define ESTADO_H
using namespace std;
#include <string>
#include "Trans.h"
class Estado
{
private:
int ident;
bool estInit;
bool estEnd;
Trans transicion1;
Trans transicion2;
public:
Estado();
~Estado();
Estado(int ident,bool inits,bool ends);
void setIdent(int id);
void setInitS(bool inits);
void setEndS(bool ends);
void setTrans1(Trans transis);
void setTrans2(Trans transis);
int getIdent();
bool getInitS();
bool getEndS();
Trans getTrans1();
Trans getTrans2();
};
#endif // TRANS_H
Estado.c
#include "Estado.h"
#include "Trans.h"
using namespace std;
Estado::Estado(int ident,bool inits,bool ends)
{
this->ident=ident;
this->estInit=inits;
this->estEnd=ends;
}
Estado::Estado()
{
estInit=false;
estEnd=false;
}
Estado::~Estado(){
}
void Estado::setIdent(int id){
ident=id;
}
void Estado::setInitS(bool inits){
estInit=inits;
}
void Estado::setEndS(bool ends){
estEnd=ends;
}
void setTrans1(Trans transis){
transicion1=new Trans();
}
void setTrans2(Trans transis){
transicion2=transis;
}
int Estado::getIdent(){
return ident;
}
bool Estado::getInitS(){
return estInit;
}
bool Estado::getEndS(){
return estEnd;
}
Trans getTrans1(){
return transicion1;
}
Trans getTrans2(){
return transicion2;
}
錯誤:
g++ -c -c -o Estado.o Estado.c
Estado.c: In function ‘void setTrans1(Trans)’:
Estado.c:30: error: ‘transicion1’ was not declared in this scope
Estado.c: In function ‘void setTrans2(Trans)’:
Estado.c:33: error: ‘transicion2’ was not declared in this scope
Estado.c: In function ‘Trans getTrans1()’:
Estado.c:45: error: ‘transicion1’ was not declared in this scope
Estado.c: In function ‘Trans getTrans2()’:
Estado.c:48: error: ‘transicion2’ was not declared in this scope
make: *** [Estado.o] Error
問題是什麼? – juanchopanza 2013-02-18 06:42:01
爲什麼你在'.c'文件中編寫C++代碼? – 2013-02-18 06:43:05
這是在學校上課嗎?如果不是,類是實現狀態機的一種非常乏味的方式。事實上,國家機器是什麼時候不使用類的招牌孩子(儘管在解析時教會學生使用的godawful訪問者模式緊隨其後:-) – 2013-02-18 06:44:08