Click here to Skip to main content
12,078,795 members (52,688 online)
Rate this:
 
Please Sign up or sign in to vote.
i have a global structure like this :-

typedef struct MachineList
{
	BSTR AccountId;
	BSTR MachineId;
	BSTR Make;
	char* Make1;
	BSTR Model;
	char* Model1;
	BSTR SerialNumber;
	BSTR IpAddress1;
	char* IpAddress;
	BSTR Port1;
	int Port;
	BSTR LocationCode;
	SOCKET Sock;
	BSTR Status;
} MACHINELIST,*PMACHINELIST;
int MachineCount=-1;

i have an xml parser like this :-
in the start of this file (which contain parse function) i added
these code
vector<struct  MACHINELIST > SS;
//i added the above code as i need to add the structure into the list
now i get the error
error C2371: 'MACHINELIST' : redefinition; different basic types
error C2512: 'MACHINELIST' : no appropriate default constructor available
void parse()
{
	{
		//Qualify namespase explicitly to avoid Compiler Error C2872 "ambiguous symbol" during linking.
		//Now Msxml2.dll use the "MSXML2" namespace
		//(see http://support.microsoft.com/default.aspx?scid=kb;en-us;316317):
		MSXML2::IXMLDOMDocumentPtr docPtr;//pointer to DOMDocument object
		MSXML2::IXMLDOMNodeListPtr NodeListPtr;//indexed access. and iteration through the collection of nodes
		MSXML2::IXMLDOMNodePtr DOMNodePtr;//pointer to the node

		MSXML2::IXMLDOMNode *pIDOMNode = NULL;//pointer to element's node
		MSXML2::IXMLDOMNode *pIParentNode = NULL;//pointer to parent node
		MSXML2::IXMLDOMNode *pIAttrNode = NULL;//pointer to attribute node
		MSXML2::IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr;//iteration through the collection of attribute nodes
		MSXML2::IXMLDOMNodeList *childList=NULL;//node list containing the child nodes

 
		//Variable with the name of node to find: 
		BSTR strFindText  = L" ";//" " means to output every node

		//Variables to store item's name, parent, text and node type:
		BSTR bstrItemText,bstrItemNode, bstrItemParent,bstrNodeType;
 
		//Variables to store attribute's name,type and text:	 
		BSTR bstrAttrName, bstrAttrType, bstrAttrText;
 
		HRESULT hResult;
 
		int i = 0;//loop-index variable
		int n = 0;//lines counter

 
		//Initialize COM Library:
		CoInitialize(NULL);
 
		//Create an instance of the DOMDocument object:
		docPtr.CreateInstance(__uuidof(DOMDocument30));
 
		// Load a document:
		_variant_t varXml(Xmlpath);//XML file to load//uncomment this
		_variant_t varResult((bool)TRUE);//result 

		varResult = docPtr->load(varXml);
 
		if ((bool)varResult == FALSE)
		{
			wLog->WriteErrorLog("failed to load XML file. Check the file name\n");
			printf("failed to load XML file. Check the file name\n");
 
			return 1;
		}
 
		//Collect all or selected nodes by tag name:
		NodeListPtr = docPtr->getElementsByTagName(strFindText);//original
		// NodeListPtr=   docPtr->documentElement->getElementsByTagName(strFindText) ;

 

		//Output the number of nodes:
		//printf("Number of nodes: %d\n", (NodeListPtr->length));

		//Output root node:
		docPtr->documentElement->get_nodeName(&bstrItemText);
		//%ls formatting is for wchar_t* parameter's type (%s for char* type):
		printf("\nRoot: %ls\n", bstrItemText);	
 
	PMACHINELIST MachineList;
 

		for(i = 0; i < (NodeListPtr->length); i++)
		{
 
			if (pIDOMNode) pIDOMNode->Release();			
			NodeListPtr->get_item(i, &pIDOMNode);
 

			if(pIDOMNode )
			{				
 
				pIDOMNode->get_nodeTypeString(&bstrNodeType);
 
				//We process only elements (nodes of "element" type): 
				BSTR temp = L"element";
 
				if (lstrcmp((LPCTSTR)bstrNodeType, (LPCTSTR)temp)==0) 
				{
					n++;//element node's number
					printf("\n\n%d\n", n);//element node's number
					printf("Type: %ls\n", bstrNodeType);
 
					pIDOMNode->get_nodeName(&bstrItemNode);
					if(0== wcscmp(bstrItemNode, L"ModuleSettings"))
					{
						isModuleSettings=TRUE;
						//MS.account_id_name
						printf("");
					}
 

					printf("Node: %ls\n", bstrItemNode);				
					if(0== wcscmp(bstrItemNode, L"Machine"))
					{
						//printf("value of count is %d\n",count);
						count++;
						//printf("value of count is %d\n",count);
						
						isMachineList=TRUE;
						
						MachineList=new MACHINELIST;
						MachineCount++;
						//MS.account_id_name

					}
					if(0== wcscmp(bstrItemNode, L"/Machine"))
					{
						printf("detected");
					}
					pIDOMNode->get_text(&bstrItemText);
					printf("Text: %ls\n", bstrItemText);
					if(isModuleSettings)//work here
					{
						if(0== wcscmp(bstrItemNode, L"capture_local_dir"))
						{
							CapturelocalDir=bstrItemText;
							printf("CapturelocalDir is %ls\n",CapturelocalDir);
						}
						if(0== wcscmp(bstrItemNode, L"capture_log_dir"))
						{
							CapturelogDir=bstrItemText;
							printf("CapturelogDir is %ls\n",CapturelogDir);
						}
						if(0== wcscmp(bstrItemNode, L"capture_log_level"))
						{
							CapturelogLevel=bstrItemText;
							printf("CapturelogLevel is %ls\n",CapturelogLevel);
						}
						if(0== wcscmp(bstrItemNode, L"capture_request_interval"))
						{
							CapturerequestInterval=bstrItemText;
							printf("CapturerequestInterval is %ls\n",CapturerequestInterval);
						}
						if(0== wcscmp(bstrItemNode, L"capture_connection_interval"))
						{
							CaptureConnectionInterval=bstrItemText;
							printf("CaptureConnectionInterval is %ls\n",CaptureConnectionInterval);
						}
					}
					if(isMachineList)
					{
					/*	PMACHINELIST MachineList;
						MachineList=new MACHINELIST;*/
						
						
						if(0== wcscmp(bstrItemNode, L"MachineId"))
						{
							m_LocalStorage.machinelist[count].MachineId=bstrItemText;
							//MachineList.MachineId=bstrItemText;
							MachineList->MachineId=bstrItemText;
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
						if(0== wcscmp(bstrItemNode, L"IpAddress"))
						{
							m_LocalStorage.machinelist[count].IpAddress=bstrItemText;
							m_LocalStorage.machinelist[count].IpAddress1=_com_util::ConvertBSTRToString(bstrItemText);
							printf("ip address1 is %s\n",m_LocalStorage.machinelist[count].IpAddress1);
							MachineList->IpAddress=_com_util::ConvertBSTRToString(bstrItemText);
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
						if(0== wcscmp(bstrItemNode, L"Port"))
						{
							m_LocalStorage.machinelist[count].Port=bstrItemText;
							m_LocalStorage.machinelist[count].Port1=_wtoi(bstrItemText);
							MachineList->Port==_wtoi(bstrItemText);
							//printf("Machine id is %ls\n",ML[count].MachineId);
							printf("port1 is %d\n",m_LocalStorage.machinelist[count].Port1);
						}
						if(0== wcscmp(bstrItemNode, L"Model"))
						{
							m_LocalStorage.machinelist[count].Model=bstrItemText;
							m_LocalStorage.machinelist[count].Model1=_com_util::ConvertBSTRToString(bstrItemText);
							MachineList->Model1=_com_util::ConvertBSTRToString(bstrItemText);
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
						if(0== wcscmp(bstrItemNode, L"Make"))
						{
							m_LocalStorage.machinelist[count].Make=bstrItemText;
							m_LocalStorage.machinelist[count].Make1=_com_util::ConvertBSTRToString(bstrItemText);
							MachineList->Make1=_com_util::ConvertBSTRToString(bstrItemText);
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
						if(0== wcscmp(bstrItemNode, L"SerialNumber"))
						{
							m_LocalStorage.machinelist[count].SerialNumber=bstrItemText;
							MachineList->SerialNumber=bstrItemText;
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
						if(0== wcscmp(bstrItemNode, L"AccountId"))
						{
							m_LocalStorage.machinelist[count].AccountId=bstrItemText;
							MachineList->AccountId=bstrItemText;
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
						if(0== wcscmp(bstrItemNode, L"location_code"))
						{
							m_LocalStorage.machinelist[count].LocationCode=bstrItemText;
							MachineList->LocationCode=bstrItemText;
							//printf("Machine id is %ls\n",ML[count].MachineId);
						}
					}
 

 

					//Get the attributes:
					int j = 0;//loop-index variable
					long length;// number of attributes in the collection

					DOMNamedNodeMapPtr = pIDOMNode->attributes;
 
					hResult = DOMNamedNodeMapPtr->get_length(&length);
 

				}
			}
		}
 
		//Do not forget to release interfaces:
		pIDOMNode->Release();
		pIDOMNode = NULL;
		//pIParentNode->Release();
		pIParentNode = NULL;
	
 
	} 
 
	catch(...)
	{
		wLog->WriteErrorLog("Exception occurred while parsing XML\n");
 
	}
 

	CoUninitialize();
 
}
Posted 30-Oct-12 7:39am
Edited 30-Oct-12 23:08pm
Jochen Arndt107.1K
v2
Comments
skydger 30-Oct-12 14:25pm
   
Try to remove 'struct' clause from your vector definition
Tarun Batra 30-Oct-12 14:35pm
   
thanks for the solution using List stl how can we get the first node of list i mean is there any function available in stl to get the node one by one
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can use iterators for any standard library containers. For example

std::list< MACHINELIST > SS;
typedef std::vector< MACHINELIST >::const_iterator cn_iterator;
//...
for( cn_iterator li = SS.begin();
                                          li != SS.end(); ++li ){
    MACHINELIST list = *li;
}
You can also use "iterator" instead or a direct access
for( size_t i = 0; i != SS.size(); i++ ){
    MACHINELIST list = SS.at(0);
}
For more information, please refer this link
http://www.cplusplus.com/reference/stl/vector/[^]
  Permalink  
Comments
Tarun Batra 30-Oct-12 15:46pm
   
sir plz do reply this http://www.codeproject.com/Questions/486436/Howpluscanplusipluspassplustheplusthisplusstlplusl
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

// your definition of your structure
typedef struct MachineList
{
    // details ...
} MACHINELIST;
 
// your vector declaration
vector<struct machinelist=""> SS;
</struct>
But MACHINELIST does not take the struct keyword, as that is already implied from the typedef. I would also question why you are using BSTR types in your structure; is this a COM library?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160212.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100