unsortedtype重载赋值运算符

时间:2016-04-18 22:05:34

标签: c++ operator-overloading

我正在使用我的调试器编写一个程序,在这个程序中,在一个充满单词的字典中检查一个单词,然后说明它是否是一个单词,并说明其含义。使用调试器时,我收到以下错误......

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

这让我相信它与我的赋值运算符重载有关。任何人都可以帮我解决这个问题吗?谢谢!下面是赋值运算符...

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);

        }
}

如果有人想看主要内容,只是想知道程序应该如何工作,这就是我写它的方式。再次感谢!

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');

}

0 个答案:

没有答案