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!

Recommended Answers

All 2 Replies

The pointers makes me weary. I don't see a reason why you need to use pointers. Remove
all instance of them, and use proper data structures like std::vector or std::list.

What type of runtime exception do you get? Stack overflow? Invalid index ? writing into memory exception?

Also, do not use code life fgets and fscan. Use proper C++ objects like cin or some iostream object.

These are the errors I get:

CXX0017: Error: symbol "node_string" not found
CXX0017: Error: symbol "no_lines" not found
CXX0017: Error: symbol "dict" not found
I tried using ifstream to read from the file, but I couldn't find a proper function similar so sscanf that will separate the whole line in words with the " " as a delimiter. Can you please suggest me something?
Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.