hi,all, I defined a struct and a non-member function with the same name and code in both main dialog class and a sub-dialog class. And I got the LNK2005 errors:
1>XMLDOMFromVCDlg.obj : error LNK2005: "struct CompareNodeLeftPosStruct cpNodeLeftPosObj" (?cpNodeLeftPosObj@@3UCompareNodeLeftPosStruct@@A) already defined in GATrainingDlg.obj 1>XMLDOMFromVCDlg.obj : error LNK2005: "int __cdecl GetNodeAttIntVal(class _com_ptr_t<class _com_IIID<struct MSXML2::IXMLDOMNode,&struct __s_GUID const _GUID_2933bf80_7b36_11d2_b20e_00c04f983e60> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?GetNodeAttIntVal@@YAHV?$_com_ptr_t@V?$_com_IIID@UIXMLDOMNode@MSXML2@@$1?_GUID_2933bf80_7b36_11d2_b20e_00c04f983e60@@3U__s_GUID@@B@@@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in GATrainingDlg.obj 1>C:\Users\daiyue\Documents\Visual Studio 2010\Projects\XMLDOMFromVC3_Home\XMLDOMFromVC2\XMLDOMFromVC\Debug\XMLDOMFromVC.exe : fatal error LNK1169: one or more multiply defined symbols found 1>
As both the main and the sub dialog class need to use the very same struct and the non-member function, so I put them in both of the dialog class cpp files.
int GetNodeAttIntVal(MSXML2::IXMLDOMNodePtr pNode, string strAttName) { MSXML2::IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr; MSXML2::IXMLDOMNodePtr DOMNodePtr; DOMNamedNodeMapPtr = pNode ->attributes; return atoi(DOMNamedNodeMapPtr->getNamedItem(_bstr_t(strAttName.c_str()))->text); }; struct CompareNodeLeftPosStruct{ bool operator()(MSXML2::IXMLDOMNodePtr a, MSXML2::IXMLDOMNodePtr b) const{ int nYVal1 = 0, nYVal2 = 0; nYVal1 = GetNodeAttIntVal(a,"ObjectRectLeft"); nYVal2 = GetNodeAttIntVal(b,"ObjectRectLeft"); // return true if 'a' should precede 'b' in your desired order, false otherwise return (nYVal1 < nYVal2); } }cpNodeLeftPosObj;
so how to fix the errors?