我在 yacc.y 中有结构错误 Struct 和队列 enQueue 定义,然后将其移至单独的 .h 文件
但它给了我链接错误,该定义在 yacc.obj 和 node.obj 中都找到了!
尝试创建新的解决方案,但仍然给出相同的错误
Error 9 error LNK2005: "class std::queue<struct errorStruct,class std::deque<struct
errorStruct,class std::allocator<struct errorStruct> > > errQueue" (?errQueue@@3V?$queue@UerrorStruct@@V?
$deque@UerrorStruct@@V?$allocator@UerrorStruct@@@std@@@std@@@std@@A) already defined in Node.obj yacc.obj
update
first :
Node.h // 用于节点类
yacc.y // 规则 + errorStruct + 队列 errQueue + 类 ErrList :包括 "Node.h" &
then:
Node.h // 对于节点类 + errorStruct + 队列 errQueue + 类 ErrList :包括
yacc.y // 规则:包括“Node.h”
update
在 Node.h 中
struct errorStruct{
int errLineNum;
int errColNum ;
char * errMessage;
};
class ErrList{
public:
void pushError(int line,int col,char * message);
void popError();
void printErrors();
int getSize();
private :
queue <errorStruct> errQueue;
};
externErrList * se = new ErrList ();
Node.h 的其余部分与此类无关
在 yacc.y 中只是使用
se->pushError(...);
并且没有类 ErrList 或 errQueue 的声明
我相信你应该将代码组织为:
yacc.h //-----> should have declaration of errQueue & errorStruct
yacc.cc //-----> should include yacc.h,
//It can create variables of type errQueue & errorStruct
node.cc //-----> should include yacc.h
//It can create variables of type errQueue & errorStruct
请注意,声明只能出现在yacc.h
并且它应该包含在需要创建上述类型实例的所有 cc 文件中,如果除了标头(yacc.h)之外,在任何 cc 文件中声明了结构,那么您最终将收到重新定义错误你提到。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)