hash_map throws run time exception

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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..)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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!
Ana Developer,

better post a compilable and runable example. It is relevant how your variables are declared. do you know the number of words in advance? what's this no_lines2 good for?

The problem might be that words_array++; goes out of bounds?

why don't you use ifstream ( http://www.cplusplus.com/reference/iostream/ifstream )?

It'd look like this

1
2
3
4
5
6
7
8
ifstream ifs(...);
std::string label;
std::string word;
while(ifs >> label >> word)
{
  dict.insert(make_pair(node_string,words_array));

}



Oh and you need to wrap your header like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef __include_sense__
#define __include_sense__

#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;
	}
        
};
#endif 
so that you don't include it twice
Last edited on
Topic archived. No new replies allowed.