2013-02-18 66 views
-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 
+1

问题是什么? – juanchopanza 2013-02-18 06:42:01

+3

为什么你在'.c'文件中编写C++代码? – 2013-02-18 06:43:05

+1

这是在学校上课吗?如果不是,类是实现状态机的一种非常乏味的方式。事实上,国家机器是什么时候不使用类的招牌孩子(尽管在解析时教会学生使用的godawful访问者模式紧随其后:-) – 2013-02-18 06:44:08

回答

6
void setTrans1(Trans transis) 

是不一样的

void Estado::setTrans1(Trans transis) 

第一声明了一个自由函数(相对于一个部件),所以类成员不能直接访问内部。