I am using hash_map<string, Word *> dict to store my Word objects. Each Word is a class that inherits from my Node class. The value in the label variable in the Word class is read from a text file. Once I start adding the objects to the dict structure, I experience a run time error in middle of execution (objects with valid labels are added to dict, but all of a sudden execution stopes after 3000 words have been added). I have noticed that in the watch window is stated that I need to overload operator=, I tried but didn't work. Below are my classes and the code for adding elements to the hashmap.
#include <stdio.h>
#include "Node.h"
#include <string>
class Word: public Node{
public:
Word& operator = (const Node &NewNode)
{
this->SetNodeID(NewNode.GetNodeID());
this->SetNodeLabel(NewNode.GetNodeLabel());
return *this;
}
Word(){};
};
#include <string>
#include <set>
class Node{
private:
std::string label;
public:
std::string GetNodeLabel() const
{
return label;
}
void SetNodeLabel(const std::string newLabelVal)
{
label=newLabelVal;
}
~Node(){};
Node(){};
Node & operator = (const Node &NewNode)
{
if(this!=&NewNode)
{
this->ID=NewNode.GetNodeID();
this->label=NewNode.GetNodeLabel();
}
return *this;
}
};
//code for setting the string in the label, label has always a correct value, but after 3000 words dict throws an exception
while(no_lines2>0) //while we read lines from the file, while loop will last
{
fgets(line_buf,100,fo);
string a="";
if(line_buf[0]==' ')
continue;
else{
sscanf(line_buf,"%s",node_char_array); //read the coordinates from the line_buf
node_string=node_char_array;
(*words_array).SetNodeLabel(node_string);
//dict[node_string]=words_array;
dict.insert(make_pair(node_string,words_array));
words_array++;
no_lines2--;
}
}
Also, I have this Sense class that inherits from Node. But if I include Sense and Word in the same c++ file, I get errors for redefinition of the vars and functions for Node. How should I solve this? Should I include all; Node, Sense and Word headers (this does not compile..)?
#include <stdio.h>
#include "Node.h"
#include <string>
class Sense: public Node{
private:
std::string exampleUse;
public:
std::string GetExampleUseString() const
{
return exampleUse;
}
void SetExampleUseString(const std::string egUse)
{
exampleUse=egUse;
}
};
Thanks a lot!