gpt4 book ai didi

c++ - C++中访问器函数的段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:52:14 25 4
gpt4 key购买 nike

我正在为 C++ 类(class)布置家庭作业,但遇到了一些困难。我的程序由于在 item::operator= 调用的 item::name() 中“使用大小为 8 的未初始化值”和“大小为 8 的无效读取”而出现段错误。

#ifndef ITEM_H
#define ITEM_H

const double WEIGHT_DEFAULT = 1.0;
const int ITEM_NAME_LENGTH = 30;

class item {
public:
// Constructors and deconstructor
item();
item(char* nme, double weight);
virtual ~item();

// Copy constructor
item(const item& itm);

// Overload assignment operator
const item& operator=(const item& itm);

// Overload inequality operator
bool operator!=(const item&) const;

// Mutators and accessors
const char* name(void) const;
double weight(void) const;
void weight(double wght);
void name(char* nme);

protected:
private:
char* m_name;
double m_weight;
};

#endif // ITEM_H

以下是我认为存在问题的实现部分:

... 

const item& item::operator=(const item& itm)
{
if (strcmp(this->name(), itm.name()) != 0)
{

this->weight(itm.weight());
strcpy(m_name, itm.name());
}

return *this;
}

const char* item::name(void) const {
return m_name;
}

...

如您所见,作业的一部分是使用 C 字符串,因此我陷入了困惑的细节。我对指针和类的理解是,在使用两者时,我们需要实现析构函数、复制构造函数和重载赋值运算符。我已经完成了上述所有操作。我在 Stack Overflow 上查看了一些关于 copy-and-swap 习语的信息,并尝试实现它,但也无法让它与我的代码一起工作。

我的大脑越来越疲惫,试图找出我哪里做错了。有人可以告诉我我缺少什么吗?

谢谢!


编辑

这里是构造函数、析构函数等:

item::item() {
//ctor

m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, " ", ITEM_NAME_LENGTH - 1);
this->weight(WEIGHT_DEFAULT);

return;
}

item::item(char* nme, double wght) {
m_name = new char[ITEM_NAME_LENGTH];

strncpy(m_name, nme, ITEM_NAME_LENGTH - 1);

// We want to check for a rational value being
// passed in to weight. In theory, an item has
// to have *some* kind of weight, so we check
// for a weight of 0. If a weight of 0 has been passed in,
// we instead initialize the item's weight to the
// default weight (set in the header file).
if (wght > 0.0) {
this->weight(wght);
} else {
this->weight(WEIGHT_DEFAULT);
}

return;
}

item::~item() {
//dtor

// TODO: We need to clean up any variables here.
delete[] m_name;
}

item::item(const item& itm) {
// copy ctor
this->weight(itm.weight());
strncpy(m_name, itm.name(), ITEM_NAME_LENGTH - 1);

}

const item& item::operator=(const item& itm)
{
if (strcmp(this->name(), itm.name()) != 0)
{

this->weight(itm.weight());
strcpy(m_name, itm.name());
}

return *this;
}

bool item::operator!=(const item& itm) const
{
return (strcmp(m_name, itm.name()) != 0);
}

编辑2

现在我已经停止尝试动态分配名称 c 字符串,我在权重函数上遇到内存错误...

double item::weight(void) const {
return m_weight;
}

void item::weight(double wght)
{
m_weight = wght;
}

最佳答案

首先,您不需要从 void 语句中明确地return;

第二个:创建复制构造函数时,您需要分配用于保存该构造函数内存的空间,这与您为常规构造函数所做的方式相同。

在你的复制构造函数中试试这个:

m_name = new char[ITEM_NAME_LENGTH];

编辑:为了完整起见,这就是我的“似乎可以工作”的代码:

#include <stdio.h>
#include <string.h>


const double WEIGHT_DEFAULT = 1.0;
const int ITEM_NAME_LENGTH = 30;

class item {
public:
// Constructors and deconstructor
item();
item(char* nme, double weight);
virtual ~item();

// Copy constructor
item(const item& itm);

// Overload assignment operator
const item& operator=(const item& itm);

// Overload inequality operator
bool operator!=(const item&) const;

// Mutators and accessors
const char* name(void) const;
double weight(void) const;
void weight(double wght);
void name(char* nme);

protected:
private:
char* m_name;
double m_weight;
};


const item& item::operator=(const item& itm)
{
if (strcmp(this->name(), itm.name()) != 0)
{

m_name = new char[ITEM_NAME_LENGTH];
this->weight(itm.weight());
strcpy(m_name, itm.name());
}

return *this;
}

const char* item::name(void) const {
return m_name;
}

double item::weight(void) const {
return m_weight;
}

void item::weight(double wght) {
m_weight = wght;
}

item::item() {
//ctor

m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, " ", ITEM_NAME_LENGTH - 1);
this->weight(WEIGHT_DEFAULT);

return;
}

item::item(char* nme, double wght) {
m_name = new char[ITEM_NAME_LENGTH];

strncpy(m_name, nme, ITEM_NAME_LENGTH - 1);

// We want to check for a rational value being
// passed in to weight. In theory, an item has
// to have *some* kind of weight, so we check
// for a weight of 0. If a weight of 0 has been passed in,
// we instead initialize the item's weight to the
// default weight (set in the header file).
if (wght > 0.0) {
this->weight(wght);
} else {
this->weight(WEIGHT_DEFAULT);
}

return;
}

item::~item() {
//dtor

// TODO: We need to clean up any variables here.
delete[] m_name;
}

item::item(const item& itm) {
// copy ctor
this->weight(itm.weight());
m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, itm.name(), ITEM_NAME_LENGTH - 1);

}

bool item::operator!=(const item& itm) const
{
return (strcmp(m_name, itm.name()) != 0);
}


int main(int argc, char* argv[])
{
item i("test",2.0);
item b = i;
item c;
item d = c;
return 0;
}

关于c++ - C++中访问器函数的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14472765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com