unsortedtype overloaded assignment operator

Hello! I'm currently using my debugger to work out a program in which a word is checked in a dictionary full of words, and then states whether it is a word or not, and states the meaning. When using the debugger, I get the following errors...

Program received signal SIGSEGV, Segmentation fault.
std::string::assign (this=0x7fffffffdfe0,
__str=<error reading variable: Cannot access memory at address 0xffffffffffffffe8>)
at /usr/src/debug/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:249
249 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());

#0 std::string::assign (this=0x7fffffffdfe0,
__str=<error reading variable: Cannot access memory at address 0xffffffffffffffe8>)
at /usr/src/debug/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:249
#1 0x0000000000401bdd in DictEntry::operator= (this=0x7fffffffdfe0) at DictEntry.h:7
#2 0x0000000000402b31 in main () at main.cpp:81

This leads me to believe that it has something to do with my assignment operator overload. Could anyone help me fix the issue? Thank you! Below is the assignment operator...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
UnsortedType & UnsortedType::operator=(const UnsortedType & right) //assignment operator overload 
{
if (this != &right) // if this object isn't the same object as the right
        {
                if (info != NULL)//It's our responsibility to set info appropriately
                        delete [] info;

                this->length = right.length;
                size = right.length;


        for (int i = 0; i < right.length; i++)
        {
        info[i] = right.info[i];
        length = size;
                //copy data
        }
        return (*this);

        }
}

In case anyone would like to see the main, just to get an idea of how the program is supposed to work, this is how I wrote it.
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
83
84
85
bool InitDictFromFile (SortedType & dict, const char *fileName)
{
  ifstream inFile;              //declare a ifstream object

  // open the file
  inFile.open (fileName);       //connect the inFile with disk file 
  // given by path name fileName

  if (!inFile)                  //if something went wrong, e.g., file nonexist,...
    {
      cerr << "failed to open " << fileName << endl;
      return false;
    }

  // keep reading a string from the file, put it into the dict
  while (!inFile.eof ())        //while we haven't reached end of file yet
    {
      string word;
      inFile >> word;

        if (inFile.eof()){
                break;
        }

        string meaning;
        getline (inFile, meaning);

        cout <<"Read word" << word << endl;
        cout <<"Read meaning" << meaning << endl;

        dict.PutItem (DictEntry (word, meaning));
 }

  inFile.close();
  dict.Print();
  return true;
}

int main ()
{
SortedType dict1, dict2;
  char cont;
  string word;
  DictEntry result;
  bool found;

  InitDictFromFile (dict1, "words.txt");
  InitDictFromFile (dict2, "cswords.txt");

 //create a dictionary that includes words from both 
 SortedType dict = dict1 + dict2;


  do {

        cout <<"Enter a word to check spell:";
        cin >> word;


        //Call the GetItem that SortedType class inherits from UnsortedType (which implements
        //"linear search") 
        result = dict.GetItem (DictEntry (word),found);
        if (!found)
                cout <<"Not a word\n";
        else
        {
                cout <<"Found the word\n";
                cout <<"Meaning " << result.GetMeaning() <<endl;
        }

        result = dict.UnsortedType::GetItem (DictEntry (word),found);
        if (!found)
                cout <<"Not a word\n";
        else
        {
                cout <<"Found the word\n";
                cout <<"Meaning " << result.GetMeaning() <<endl;
        }

        cout <<"Continue?";
        cin >> cont;

  } while (cont=='Y' || cont=='y');

}
Last edited on
1
2
3
4
5
6
7
8
9
if (info != NULL) // It's our responsibility to set info appropriately
  delete[] info;

//here `info' is either NULL, or points to deleted memory
this->length = right.length;
size = right.length;

for (int i = 0; i < right.length; i++) {
  info[i] = right.info[i]; //and here you are trying to dereference it 
Topic archived. No new replies allowed.