如何正确使用对象中的整数和浮点数?

时间:2016-06-24 05:50:42

标签: c++ floating-point integer dynamic-memory-allocation

所以我试图编写一个程序来跟踪具有以下属性的酒厂的各种属性。

- 动态分配内存

- 跟踪酿酒厂开始的年份

- 成功评级和葡萄园占用的土地面积的浮动。

- 添加,删除,显示(按评级和名称)并保存酒厂数据的功能。

我已经编写/调试过代码来处理类似的程序,其中所有属性都是cstrings,而且这段代码基于相同的框架。

我的问题是,适用于动态分配的cstrings的相同代码不适用于整数或浮点数,而且我从编译器获取转换错误消息。我有一个包含list的{​​{1}}类,因为我使用私有成员来隐藏数据,我将函数从客户端程序传递到aWinery和{{{ 1}}。具有相同类型的函数是相同的,因此我在此处仅包含每种类型中的一种。

有人可以指出我做错了什么吗?如果list的int和float的定义不是指针,我不知道如何返回参数。我可以通过类找到的动态内存编程的唯一示例使用cstrings。我已经注意到我从教师那里复制的代码中有几点,因为我无法弄明白。

编辑:我得到的具体错误是aWinery,其中某些排列发生在所有aWinery的整数和基于浮点的访问器和增变器函数中。

List Accessor,Mutator,Constructor和Destructor函数

invalid conversion from int* to int\n year = this->year

aWinery Accessor,Mutator,Constructor和Destructor函数

aWinery

aWinery头文件

void list::getWineryLocation(char location[]) const
{
    wineryData.getLocation(location);
}

void list::getWineryYear(int year) const
{
    wineryData.getYear(year);
}

void list::getWineryAcres(float acres) const
{
    wineryData.getAcres(acres);
}

void list::setWineryLocation(char location[])
{
    wineryData.setLocation(location);
}

void list::setWineryYear(int year)
{
    wineryData.setYear(year);
}

void list::setWineryAcres(float acres)
{
    wineryData.setAcres(acres);
}

//Constructor functions

list::list()
{
    nameHead = NULL;
    nameTail = NULL;
    ratingHead = NULL;
    ratingTail = NULL;
    size = 0;
}

//Destructor
//Doesn't delete the head/tailRating pointers to avoid double deleting a winery
list::~list()
{
    node * curr = nameHead;

    while (nameHead != NULL)
    {
        curr = nameHead->nextByName;

        delete nameHead;

        nameHead = curr;
    }
}

列出头文件

//Winery object constructor
aWinery::aWinery()
{
    name = new char[strlen("Unknown")+1];
    strcpy(name, "Unknown");
    location = new char[strlen("Unknown")+1];
    strcpy(location, "Unknown");
    year = new int;
    year = 0;
    acres = new float;
    acres = 0;
    successRating = new float;
    successRating = 0;
}

//I have no idea whats going on here
//Winery destructor
aWinery::~aWinery()
{   
    if(name != NULL)
        delete [] name;
    if(location != NULL)
        delete [] location;
    if(year != 0)
        delete year;
    if(acres != 0)
        delete acres;
    if(successRating != 0)
        delete successRating;
}

void aWinery::getLocation(char location[]) const
{
    strcpy(location, this->location);
}

void aWinery::getYear(int year) const
{
    year = this->year;
}

void aWinery::getAcres(float acres) const
{
    acres = this->acres;
}

//I have no idea why this is written this way, I copied this from an instructor example
void aWinery::setLocation(char location0[])
{
    if(this->location != NULL)
        delete [] this->location;
    this->location =  new char[strlen(location0)+1];

    strcpy(this->location, location0);
}

void aWinery::setYear(int year0)
{
    if(this->year != 0)
        delete this->year;
    this->year =  new int;

    this->year = year0;
}

void aWinery::setAcres(float acres0)
{
    if(this->acres != 0)
        delete this->acres;
    this->acres =  new float;

    this->acres = acres0;
}

1 个答案:

答案 0 :(得分:4)

我认为你的大部分问题都在标题中,在这里:

private:  
   char* name;
   char* location;
   int* year;
   float* acres;
   float* successRating;

没有理由为任何这些成员变量使用指针;你不妨让他们成为所有普通的按价值成员,如下:

private:  
   std::string name;
   std::string location;
   int year;
   float acres;
   float successRating;

请注意,我用std :: string替换了(char *)成员变量,因为在C ++程序中,没有理由不使用正确的字符串对象。 (如果你用C语言编写,使用动态分配的char数组来保存字符串可能是必要的恶,但在C ++中它只是受虐狂)

通过按值存储所有数据,您可以删除所有newdelete命令(以及随之而来的不可避免的错误和低效率),并设置并获取必要时直接使用这些值。

此外,此访问器功能错误:

void aWinery::getYear(int year) const
{
    year = this->year;
}

它不起作用,因为在C ++中,方法参数(默认情况下)是按值传递的(即传入值的临时本地副本,对于要使用的方法),所以上面的所有方法都可以修改本地参数year,然后在函数返回后立即销毁。一个合适的访问器方法看起来更像是这样:

int aWinery::getYear() const
{
    return this->year;   // assuming year member variable is now of type int
}

....同样,setter方法应如下所示:

void aWinery::setYear(int newYear)
{
    this->year = newYear;   // assuming year member variable is now of type int
}