链接 error2005 Visual Studio 2008 c++

发布于 2024-11-07 09:42:08 字数 1264 浏览 5 评论 0原文

我有 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 using
se->pushError(...);
and as no declaration of class ErrList or errQueue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-11-14 09:42:08

我相信您应该将代码组织为:

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 文件中都声明了结构,那么您最终将收到您提到的重新定义错误。

I believe you should organize the code as:

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

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.

空袭的梦i 2024-11-14 09:42:08

哎哟!我忘了发布答案..抱歉..

在@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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文