链接 error2005 Visual Studio 2008 c++
我有 struct errorStruct & yacc.y 中的队列 errQueue 定义,然后将其移至单独的 .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
更新
:
Node.h // 用于节点类
yacc.y // 规则 + errorStruct + 队列 errQueue + ErrList 类:包括“Node.h” & <队列>
然后:
Node.h // 对于节点类 + errorStruct + 队列 errQueue + 类 ErrList :包含 <队列>
yacc.y // 规则:
包含“Node.h”更新
在 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 的声明
I had struct errorStruct & a queue errQueue definition in yacc.y , then moved it to separate .h file
but it gives me linking error that the definition is found in both yacc.obj and node.obj !!
tried creating new solution but still gives the same error
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 // for node class
yacc.y // rules + errorStruct + queue errQueue + class ErrList : includes "Node.h" & < queue>
then:
Node.h // for node class + errorStruct + queue errQueue + class ErrList : includes < queue>
yacc.y // rules : includes "Node.h"
update
in 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 ();
the rest of Node.h has nothing to do with this class
in yacc.y just usingse->pushError(...);
and as no declaration of class ErrList or errQueue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您应该将代码组织为:
请注意,声明应该只出现在 yacc.h 中,并且它应该包含在需要创建所述类型实例的所有 cc 文件中,如果除了标头(yacc.h)之外,您的任何 cc 文件中都声明了结构,那么您最终将收到您提到的重新定义错误。
I believe you should organize the code as:
Note that the declarations should only be present in
yacc.h
and it should be included in all your cc files which need to create instances of the said types, If the structures are declared in any of your cc file in addition to the header(yacc.h) then you will end up getting the redefinition errors you mentiones.哎哟!我忘了发布答案..抱歉..
在@Peter K.的参考的帮助下得到了它:
转到VS:项目->属性页->配置属性->链接器 -> 命令行
并在附加选项框中添加
/FORCE:MULTIPLE
opss! I forgot to post the answer .. sorry ..
got it with the help of @Peter K.'s reference :
go to VS : project -> property page -> configuration properties -> linker ->command line
and add
/FORCE:MULTIPLE
in additional options box