变量声明 - 链接错误 2005 Visual Studio 2008 C++

variable declaration - linking error2005 visual studio 2008 c++

本文关键字:Visual Studio 2008 C++ 2005 错误 声明 链接 变量      更新时间:2023-10-16

我在yacc.y中有struct errorStruct和一个queue 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//for node class
yacc.y//rules + errorStruct + queue errQueue + class ErrList : 包括 "Node.h" &

然后:

Node.h//for node class + errorStruct + queue errQueue + class ErrList : 包括<队列>
yacc.y//规则:包括 "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 文件中,如果除了 header(yacc.h) 之外,在任何 cc 文件中声明了结构,那么你最终会得到你提到的重定义错误。

opss!我忘了发布答案..不好意思。。

在@Peter K.的参考的帮助下得到了它:

转到 VS:项目 -> 属性页 ->

配置属性 -> 链接器 ->命令行

并在其他选项框中添加/FORCE:MULTIPLE