交换方法有问题
Trouble with swap method
我尝试编写的交换方法有问题。我确信这是这部分代码,因为如果这些行未被注释,它只会在程序终止时抛出异常。将它们注释掉,文件将正确终止。
我的 类 和我遇到问题的功能如下。
class WordOccurrence {
public:
//Constructor
WordOccurrence(const std::string& word = "", int num = 0) { num_ = num; word_ = word; };
//Member Functions
bool matchWord(const std::string &); // returns true if word matches stored
void increment(); // increments number of occurrences
//Accessors
std::string getWord() const;
int getNum() const;
private:
std::string word_;
int num_;
};
//Bag
class WordList {
public:
//Big 3:
WordList(int size = 0) { size_ = size; wordArray_ = size>0 ? new WordOccurrence[size] : nullptr;};
~WordList() { delete[] wordArray_; };
WordList(const WordList& list);
//Assignment Overload
WordList& operator =(const WordList& source);
//Member Functions
void addWord(const std::string &word);
friend void swap(WordOccurrence& first, WordOccurrence& second);
// void swap(WordOccurrence& lhs, WordOccurrence& rhs);
void sortList();
void printList();
private:
WordOccurrence *wordArray_; // a dynamically allocated array of WordOccurrences
// may or may not be sorted
int size_;
};
和包含交换的排序函数:
void WordList::sortList() {
for (int i = 0; i < size_; ++i) {
for (int j = size_; j > i; --j) {
if (wordArray_[j].getNum() < wordArray_[j - 1].getNum()) {
WordOccurrence tmp(wordArray_[j].getWord(), wordArray_[j].getNum()); //problem is
// tmp = wordArray_[j]; // is
wordArray_[j] = wordArray_[j-1]; // in
wordArray_[j-1] = tmp; // here
//swap(wordArray_[j], wordArray_[j - 1]);
}
}
}
}
我也尝试将 'tmp' 初始化为一个空对象,但这也没有什么不同。
我也试过 std::swap 并且当程序终止时它抛出相同的 "triggered a breakpoint" 错误。同样,如果我注释掉问题行,错误就会消失。如有任何帮助,我们将不胜感激!
通过检查代码,size_
成员指定动态分配的大小 wordArray_
。
for (int j = size_; j > i; --j) {
if (wordArray_[j].getNum() < wordArray_[j - 1].getNum()) {
这将 运行 超出数组末尾,导致未定义的行为,并可能崩溃。
j
开始等于 size_
。由于 size_
是 wordArray_
的实际大小,并且 wordArray_
包含编号为 0 到 size_-1
的元素,因此 wordArray_[j]
在第一次迭代中不存在。那是你的错误。
我尝试编写的交换方法有问题。我确信这是这部分代码,因为如果这些行未被注释,它只会在程序终止时抛出异常。将它们注释掉,文件将正确终止。 我的 类 和我遇到问题的功能如下。
class WordOccurrence {
public:
//Constructor
WordOccurrence(const std::string& word = "", int num = 0) { num_ = num; word_ = word; };
//Member Functions
bool matchWord(const std::string &); // returns true if word matches stored
void increment(); // increments number of occurrences
//Accessors
std::string getWord() const;
int getNum() const;
private:
std::string word_;
int num_;
};
//Bag
class WordList {
public:
//Big 3:
WordList(int size = 0) { size_ = size; wordArray_ = size>0 ? new WordOccurrence[size] : nullptr;};
~WordList() { delete[] wordArray_; };
WordList(const WordList& list);
//Assignment Overload
WordList& operator =(const WordList& source);
//Member Functions
void addWord(const std::string &word);
friend void swap(WordOccurrence& first, WordOccurrence& second);
// void swap(WordOccurrence& lhs, WordOccurrence& rhs);
void sortList();
void printList();
private:
WordOccurrence *wordArray_; // a dynamically allocated array of WordOccurrences
// may or may not be sorted
int size_;
};
和包含交换的排序函数:
void WordList::sortList() {
for (int i = 0; i < size_; ++i) {
for (int j = size_; j > i; --j) {
if (wordArray_[j].getNum() < wordArray_[j - 1].getNum()) {
WordOccurrence tmp(wordArray_[j].getWord(), wordArray_[j].getNum()); //problem is
// tmp = wordArray_[j]; // is
wordArray_[j] = wordArray_[j-1]; // in
wordArray_[j-1] = tmp; // here
//swap(wordArray_[j], wordArray_[j - 1]);
}
}
}
}
我也尝试将 'tmp' 初始化为一个空对象,但这也没有什么不同。 我也试过 std::swap 并且当程序终止时它抛出相同的 "triggered a breakpoint" 错误。同样,如果我注释掉问题行,错误就会消失。如有任何帮助,我们将不胜感激!
通过检查代码,size_
成员指定动态分配的大小 wordArray_
。
for (int j = size_; j > i; --j) {
if (wordArray_[j].getNum() < wordArray_[j - 1].getNum()) {
这将 运行 超出数组末尾,导致未定义的行为,并可能崩溃。
j
开始等于 size_
。由于 size_
是 wordArray_
的实际大小,并且 wordArray_
包含编号为 0 到 size_-1
的元素,因此 wordArray_[j]
在第一次迭代中不存在。那是你的错误。