【发布时间】:2013-04-26 18:40:04
【问题描述】:
我正在创建一个函数,当我的 Student 类中的两个对象被重载时,它会重载 + 运算符。该函数应该添加他们的年龄和身高(类的两个受保护的数据字段)。然后它调用构造函数来创建一个具有这些字段的新学生。这也是模板中的练习,因此无法删除这些字段。
当我编译我的程序时,我在运行时遇到了分段错误。使用 cout 语句,我可以看到正在创建新的 Student 并且退出了构造函数,但是随后发生了分段错误。我意识到这一定是内存问题,但我无法找到解决方案。我曾尝试使用动态内存在重载运算符和主函数中创建新学生,但错误仍然存在。
这里是构造函数:
template <typename T>
Student<T>::Student(int age, int height)
{
this->age = age;
this->height = height;
cout << "New student created"<< endl;
return;
}
这里是重载的操作符函数:
template<typename T>
Student<T> Student<T>::operator+(Student<T> &secondStudent) const
{
int a = age + secondStudent.getAge();
int h = height + secondStudent.getHeight();
new Student(a, h);
}
这里是主要功能:
Student<int> *s2 = new Student<int>(15, 63);
Student<int> *s3 = new Student<int>(18, 72);
Student <int> s4 = (*s2+ *s3);
cout << "did it return?" << endl;
注意,两个cout语句是打印的,所以我知道调用了操作符,创建了student,但是后来遇到了内存问题。
【问题讨论】:
-
打开编译器的警告。另请注意,您几乎不应该在 C++ 中使用 new 和 delete,更不用说一旦
make_unique到达。 -
不是你问的,但请放弃指针
Student<int> s2(15, 63); Student<int> s3(18, 72); Student<int> s4 = s2 + s3;。没有它们,编程会容易得多。 -
您似乎很难将编程实践从 Java 音译为 C++。在 C++ 中,您应该很少使用
new关键字。
标签: c++