noob在这里。我正在从书中做练习,编译器报告没有错误,但是当我尝试运行它时程序崩溃了。
我正在尝试运行一个运行Cow类不同方法的程序。它明确地具有:构造函数,默认构造函数,复制构造函数,析构函数,重载赋值运算符以及显示其内容的方法。
我将完成整个项目:
班级规范:
//cow.h -- For project Exercise 12.1.cbp
class Cow
{
char name[20]; // memory is allocated in the stack
char *hobby;
double weight;
public:
Cow();
Cow(const char *nm, const char *ho, double wt);
Cow(const Cow &c);
~Cow();
Cow & operator=(const Cow &c);
void ShowCow() const; // display all cow data
};
方法实施:
// cow.cpp -- Cow class methods implementation (compile with main.cpp)
#include <cstring>
#include <iostream>
#include "cow.h"
Cow::Cow() // default destructor
{
strcpy(name, "empty");
hobby = new char[6]; // makes it compatible with delete[]
strcpy(hobby, "empty");
weight = 0.0;
}
Cow::Cow(const char *nm, const char *ho, double wt)
{
strcpy(name, nm); // name = nm; is wrong, it copies the address of the argument pointer (swallow copying)
/*if (name[20] != '\0') // if it's not a string, make it a string (in case nm is larger than 20)
name[20] = '\0';*/
hobby = new char[strlen(ho) + 1]; // allocates the needed memory to hold the argument string
strcpy(hobby, ho); // copies the pointed-to data from the argument pointer to the class pointer
weight = wt;
}
Cow::Cow(const Cow &c) // copy constructor
{
strcpy(name, c.name); // copies the value to the desired address
char *temp = hobby; // stores the address of the memory previously allocated with new
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby, c.hobby); // copies the value to the new address
delete[] temp; // deletes the previously new allocated memory
weight = c.weight;
}
Cow::~Cow()
{
delete[] hobby;
}
Cow & Cow::operator=(const Cow &c) // overloaded assignment operator
{
strcpy(name, c.name);
char *temp = hobby;
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby, c.hobby);
delete[] temp;
weight = c.weight;
return *this;
}
void Cow::ShowCow() const
{
std::cout << "Name: " << name << '\n';
std::cout << "Hobby: " << hobby << '\n';
std::cout << "Weight: " << weight << "\n\n";
}
客户端:
// main.cpp -- Exercising the Cow class (compile with cow.cpp)
#include "cow.h"
#include <iostream>
int main()
{
using std::cout;
using std::cin;
Cow subject1; // default constructor
Cow subject2("Maria", "Reading", 120); // non-default constructor
Cow subject3("Lula", "Cinema", 135);
subject1 = subject3; // overloaded assignment operator
Cow subject4 = subject2; // copy constructor
subject1.ShowCow();
subject2.ShowCow();
subject3.ShowCow();
subject4.ShowCow();
cin.get();
return 0;
}
我隐藏了代码的某些部分以找到可能出现的问题,似乎编程不喜欢这两行:
subject1 = subject3;
Cow subject4 = subject2
特别是在重载赋值运算符和复制构造函数中,如果我隐藏delete[] temp
行,程序不会崩溃。
我完全是菜鸟,可能是愚蠢的,但我看不出我在这些定义中做错了什么。
任何帮助?
答案 0 :(得分:3)
Cow::Cow(const Cow &c) // copy constructor
{
strcpy(name, c.name); // copies the value to the desired address
char *temp = hobby; // stores the address of the memory previously allocated with new
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby, c.hobby); // copies the value to the new address
delete[] temp; // deletes the previously new allocated memory
weight = c.weight;
}
这是复制品。以前没有分配成员。 this->hobby
指向随机垃圾。因此,当您delete[] temp
(即新作业之前的this->hobby
)时,您会获得UB。
Cow & Cow::operator=(const Cow &c) // overloaded assignment operator
{
strcpy(name, c.name);
char *temp = hobby;
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby, c.hobby);
delete[] temp;
hobby = name;
weight = c.weight;
return *this;
}
hobby = name
不正确,因为它会导致内存泄漏+析构函数将尝试删除未使用operator new[]
分配的对象。
答案 1 :(得分:0)
@ForEveR已经注意到你的拷贝构造函数正在删除从未分配过的内存。
但是我会争辩说整个练习都是有缺陷的,并且用C语言教C语言,而不是C ++。如果您使用字符串,那么您的所有问题都会消失,因为您不必直接管理资源。这样就无需完全编写析构函数或复制赋值/构造函数。例如:
class Cow
{
std::string name;
std::string hobby;
double weight;
public:
Cow();
Cow(const std::string& nm, const std::string& ho, double wt);
void ShowCow() const; // display all cow data
};