我试图在默认构造函数中将一个char数组设置为等于null('\ 0')。我尝试在没有传递任何参数的情况下将已初始化的char指针(类的受保护成员)的值设置为null。如果传递了正确的参数,则该程序可以正常运行,但是当我在默认构造函数中设置它们时,出现了段错误:11错误。
这是头文件:
#ifndef _DVD_H_
#define _DVD_H_
class DVD {
protected:
unsigned int id;
char* title;
char* director;
char* makecopy(const char*);
public:
DVD(unsigned int i, const char* t, const char* dir);
void display();
DVD ()
{
const char temp[] = {'\0'};
id = 0;
title = makecopy(temp);
director = makecopy(temp);
}
};
#endif /* _DVD_H_ */
这是.cpp文件:
#include <iostream>
#include "DVD.h"
using namespace std;
DVD::DVD (unsigned int i, const char* t, const char* dir)
{
//Constructing id
id = i;
//Constructing title
title = makecopy(t);
//Constructing director
director = makecopy(dir);
}
void DVD::display()
{
//Creating variables to track the length of the title/director and the location of the space.
int len_t,len_dir,space_dir;
for (len_t = 0; title[len_t] != '\0'; len_t++)
;
for (len_dir = 0; director[len_dir] != '\0'; len_dir++)
;
for (space_dir = 0; director[space_dir] != ' '; space_dir++)
;
//Display the information in the desired format.
cout << '[' << id << ". " << title << '/';
for (int p = 0; p < space_dir; p++)
cout << director[p];
for (int j = space_dir; j < len_dir; j++)
cout << director[j];
cout << ']' << endl;
}
char* DVD::makecopy(const char* str)
{
int len;
char* copy;
for (len = 0; str[len] != '\0'; len++)
;
copy = new char[len+1];
for (int i = 0; i < len+1; i++)
copy[i] = str[i];
return copy;
}
这是另一个.cpp文件
#include<iostream>
using namespace std;
#include"DVD.h"
int main()
{
char str[] = "Gandhi";
DVD d1(4, str, "Richard Attenborough");
DVD d2;
cout << "First step: " << endl;
cout << "After constructors:" << endl;
d1.display(); cout << endl; // [4. Gandhi/Richard Attenborough]
d2.display(); cout << endl; // [0. /]
cout << "Test for dynamically allocated copies" << endl;
d1.display(); cout << endl; // [4. Gandhi/Richard Attenborough]
d2.display(); cout << endl; // [0. /]
}
最佳答案
您没有实现Rule of Three,并且您的makecopy()
可疑。
尝试类似这样的方法:
#ifndef _DVD_H_
#define _DVD_H_
class DVD
{
protected:
unsigned int id;
char* title;
char* director;
static char* makecopy(const char* str);
public:
DVD();
DVD(unsigned int i, const char* t, const char* dir);
DVD(const DVD &src);
~DVD();
DVD& operator=(const DVD &rhs);
void display() const;
};
#include "DVD.h"
#include <iostream>
DVD::DVD()
: id(0), title(NULL), director(NULL)
{
}
DVD::DVD(unsigned int i, const char* t, const char* dir)
id(i), title(makecopy(t)), director(makecopy(dir))
{
}
DVD::DVD(const DVD& src)
id(src.id), title(makecopy(src.title)), director(makecopy(src.director))
{
}
DVD::~DVD()
{
delete[] title;
delete[] director;
}
DVD& DVD::operator=(const DVD& rhs)
{
if (this != &rhs)
{
DVD copy(rhs);
id = copy.id;
char *c_temp = copy.title;
copy.title = title;
title = c_temp;
c_temp = copy.director;
copy.director = director;
director = c_temp;
}
return *this;
}
void DVD::display() const
{
//Display the information in the desired format.
std::cout << '[' << id << ". " << title << '/' << director << ']' << std::endl;
}
char* DVD::makecopy(const char* str)
{
int len = 0;
if (str)
{
while (str[len] != '\0')
++len;
}
char* copy = new char[len+1];
for (int i = 0; i < len; ++i)
copy[i] = str[i];
copy[len] = '\0';
return copy;
}
话虽如此,我强烈建议您改用
std::string
。让它和编译器为您处理所有内存管理:#ifndef _DVD_H_
#define _DVD_H_
#include <string>
class DVD
{
protected:
unsigned int id;
std::string title;
std::string director;
public:
DVD();
DVD(unsigned int i, const std::string& t, const std::string& dir);
void display() const;
};
#endif /* _DVD_H_ */
#include "DVD.h"
#include <iostream>
DVD::DVD()
: id(0)
{
}
DVD::DVD(unsigned int i, const std::string& t, const std::string& dir)
: id(i), title(t), director(dir)
{
}
void DVD::display() const
{
//Display the information in the desired format.
std::cout << '[' << id << ". " << title << '/' << director << ']' << std::endl;
}