linking error2005 visual studio 2008 c++

风格不统一 提交于 2020-01-06 05:15:07

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6020559/linking-error2005-visual-studio-2008-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!