在 C++ 中将复制构造函数 and/or 赋值运算符与字符串一起使用时发生堆栈溢出
stack overflow when using copy constructor and/or assignment operator with string in c++
我正在尝试 运行 对我自己制作的简单 class 进行插入排序,它具有几个字段(int、float 和 string)以及一个复制构造函数,赋值运算符和“>”运算符。
但是,当我 运行 下面的代码时出现堆栈溢出。 visual studio 告诉我它来自 'Student' class 中的 getName() 函数。错误源于我的插入排序函数中的赋值 arr[i + 1] = arr[i];
有人知道这是为什么吗?我对 C++ 比较陌生,主要来自 java 背景。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
public:
Student(string nm, int ID, float avg): name(nm), studentID(ID), overallAverage(avg) {}
Student(const Student& rhs) : name(rhs.getName()), studentID(rhs.getStudentID()), overallAverage(rhs.getOverallAverage()) {}
// copy and swap idiom
Student& operator=(const Student& rhs) {
Student copy = rhs; // places a copy of rhs into copy using the copy constructor (copy will be cleaned up on return)
swap(*this, copy); // move copy or rhs into this, this is a pointer to current object, *this dereferences the pointer
return *this;
}
~Student() {}
bool operator>(const Student& rhs) {
if (rhs.getOverallAverage() > overallAverage)
return false;
else return true;
}
string getName()const { return name; }
int getStudentID()const { return studentID; }
float getOverallAverage()const { return overallAverage; }
private:
string name;
int studentID;
float overallAverage;
};
template<typename T>
vector<T>& insertionSort(vector<T>& arr){
for (int j = 1; j < arr.size(); j++) {
T key = arr[j];
int i = j - 1;
while (i > -1 && arr[i] > key) {
arr[i + 1] = arr[i];
i = i - 1;
}
arr[i + 1] = key;
}
return arr;
}
int main()
{
vector<Student> students = {Student("russ",0,89),Student("Seb",1,75),Student("julia",2,85),
Student("johnny",3,90),Student("Sushma",4,55)};
students = insertionSort(students);
for (int i = 0; i < students.size(); i++) {
cout << students[i].getName() << ", ";
}
cout << endl;
}
原始操作员= 来自我正在使用的 txtbook:
IntCell & operator= ( const IntCell & rhs ) // Copy assignment
{
IntCell copy = rhs;
std::swap( *this, copy );
return *this;
}
无限递归是由于std::swap
调用Student::operator=
,调用std::swap
,调用Student::operator=
,等等
为了缓解这个问题,编写您自己的 swap
函数,在每个成员上调用 std::swap
:
class Student
{
//...
void swap(Student& left, Student& right)
{
std::swap(left.name, right.name);
std::swap(left.studentID, right.studentID);
std::swap(left.overallAverage, right.overallAverage);
}
//...
};
我正在尝试 运行 对我自己制作的简单 class 进行插入排序,它具有几个字段(int、float 和 string)以及一个复制构造函数,赋值运算符和“>”运算符。
但是,当我 运行 下面的代码时出现堆栈溢出。 visual studio 告诉我它来自 'Student' class 中的 getName() 函数。错误源于我的插入排序函数中的赋值 arr[i + 1] = arr[i];
有人知道这是为什么吗?我对 C++ 比较陌生,主要来自 java 背景。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
public:
Student(string nm, int ID, float avg): name(nm), studentID(ID), overallAverage(avg) {}
Student(const Student& rhs) : name(rhs.getName()), studentID(rhs.getStudentID()), overallAverage(rhs.getOverallAverage()) {}
// copy and swap idiom
Student& operator=(const Student& rhs) {
Student copy = rhs; // places a copy of rhs into copy using the copy constructor (copy will be cleaned up on return)
swap(*this, copy); // move copy or rhs into this, this is a pointer to current object, *this dereferences the pointer
return *this;
}
~Student() {}
bool operator>(const Student& rhs) {
if (rhs.getOverallAverage() > overallAverage)
return false;
else return true;
}
string getName()const { return name; }
int getStudentID()const { return studentID; }
float getOverallAverage()const { return overallAverage; }
private:
string name;
int studentID;
float overallAverage;
};
template<typename T>
vector<T>& insertionSort(vector<T>& arr){
for (int j = 1; j < arr.size(); j++) {
T key = arr[j];
int i = j - 1;
while (i > -1 && arr[i] > key) {
arr[i + 1] = arr[i];
i = i - 1;
}
arr[i + 1] = key;
}
return arr;
}
int main()
{
vector<Student> students = {Student("russ",0,89),Student("Seb",1,75),Student("julia",2,85),
Student("johnny",3,90),Student("Sushma",4,55)};
students = insertionSort(students);
for (int i = 0; i < students.size(); i++) {
cout << students[i].getName() << ", ";
}
cout << endl;
}
原始操作员= 来自我正在使用的 txtbook:
IntCell & operator= ( const IntCell & rhs ) // Copy assignment
{
IntCell copy = rhs;
std::swap( *this, copy );
return *this;
}
无限递归是由于std::swap
调用Student::operator=
,调用std::swap
,调用Student::operator=
,等等
为了缓解这个问题,编写您自己的 swap
函数,在每个成员上调用 std::swap
:
class Student
{
//...
void swap(Student& left, Student& right)
{
std::swap(left.name, right.name);
std::swap(left.studentID, right.studentID);
std::swap(left.overallAverage, right.overallAverage);
}
//...
};