Re: Iterator destructor problem
vivekian wrote:
[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :
I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
You mean <iostream>. There is no <iostream.h> in the C++ standard.
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;
This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.
childInfo () {} ;
~childInfo () {} ;
};
class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
~nodeInfo () {} ;
};
class NodeManager
{
private:
std::map <std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();
void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
void deleteRoute (const std::string childMacAddr,const std::string
meshId);
void getRoute (const std::string macAddr,const std::string
&meshId);
};
#endif /*NODEMANAGER_H_*/
//NodeManager.cpp
//---------------
#include "NodeManager.h"
NodeManager::NodeManager()
{
}
NodeManager::~NodeManager()
{
}
void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::vector<childInfo> trial ;
std::map <std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
iNode -> second.meshId = meshId ;
}
std::map <std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
childInfo c ;
c.relativeMeshId = atoi
(meshId.substr(meshId.length()-1,1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
// main.cpp
#include "LibMgr/NodeManager.h"
int main ( int argc , char *argv[] )
{
NodeManager n ;
n.addRoute("" , "a" ,"11") ;
n.addRoute("" , "b" ,"12") ;
n.addRoute("" , "c" ,"13") ;
n.addRoute("a", "d" ,"111") ;
n.addRoute("a", "e" ,"112") ;
n.addRoute("b", "f" ,"121") ;
n.addRoute("c", "g" ,"131") ;
n.addRoute("c", "h" ,"132") ;
n.addRoute("c", "i" ,"133") ;
}
// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault
Thanks,
Vivekian