【发布时间】:2016-06-24 05:50:42
【问题描述】:
所以我正在尝试编写一个程序来跟踪具有以下属性的酒厂的各种属性。
-动态分配内存
-跟踪酿酒厂开始年份的整数
-成功评级和葡萄园占用的土地面积的浮动。
-添加、删除、显示(按评级和名称)和保存酒厂数据的功能。
我已经编写/调试过代码以使用一个类似的程序,其中所有属性都是 cstrings,并且此代码基于相同的框架。
我的问题是适用于动态分配的 cstrings 的相同代码不适用于整数或浮点数,并且我从编译器收到转换错误消息。我有一个包含aWinery 的list 类,因为我使用私有成员来隐藏数据,所以我有函数可以从客户端程序传递到list 和aWinery。相同类型的函数是相同的,所以我在这里只包括每种类型的一种。
有人能指出我做错了什么吗?如果aWinery 的int 和float 定义不是指针,我不知道如何通过参数返回。我可以在类中找到的唯一动态内存编程示例使用 cstrings。我在从讲师那里复制的代码中注意到了几点,因为我无法弄清楚。
编辑:我得到的具体错误是invalid conversion from int* to int\n year = this->year,在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;
}
}
aWinery Accessor、Mutator、Constructor 和 Destructor 函数
//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;
}
aWinery 头文件
#ifndef AWINERY_H
#define AWINERY_H
#include <iostream>
using namespace std;
//winery object
class aWinery
{
public:
//Constructor
aWinery();
//Destructor
~aWinery();
//Accessor Prototypes
void getName(char name[]) const;
void getLocation(char location[]) const;
void getYear(int year) const;
void getAcres(float acres) const;
void getSuccessRating(float successRating) const;
//Mutator Prototypes
void setName(char name0[]);
void setLocation(char location0[]);
void setYear(int year0);
void setAcres(float acres0);
void setSuccessRating(float successRating0);
private:
char* name;
char* location;
int* year;
float* acres;
float* successRating;
};
#endif
列出头文件
#ifndef ALIST_H
#define ALIST_H
#include <iostream>
using namespace std;
const int MAX_CHAR_LENGTH = 1000;
class list
{
public:
//Prototypes
void setInWinery(char name[], char location[], int year, float acres, float successRating);
void initializeDbase(char savePathName[]);
void printEntireDbase();
void deleteWinery(char nameOfWinery[]);
void whereIsWinery(char nameOfWinery[]);
void save(char savePathName[]);
//Accessor Prototypes
void getWineryName(char name[]) const;
void getWineryLocation(char location[]) const;
void getWineryYear(int year) const;
void getWineryAcres(float acres) const;
void getWinerySuccessRating(float successRating) const;
int getSize() const;
//Mutator Prototypes
void setWineryName(char name[]);
void setWineryLocation(char location[]);
void setWineryYear(int year);
void setWineryAcres(float acres);
void setWinerySuccessRating(float successRating);
//Constructor Prototype
list();
//Destructor
~list();
private:
//Wine object
struct node
{
aWinery wineryData;
node * nextByName;
node * nextByRating;
};
node * nameHead, * nameTail, * ratingHead, * ratingTail;
aWinery wineryData;
int size;
};
#endif
【问题讨论】:
-
"有人能指出我做错了什么吗?" - 也许课程?您的讲师不应该教您分配自己的字符数组,使用 strlen/strcpy 等 - 使用
std::string。最好不要将列表功能与您的数据类型混合使用——做好一件事,您可以独立测试和理解它,然后重用它。std::list已经很好地列出了列表。不要使用int*和float*来存储单个值:只需按值直接存储它们。最后,对于本网站 - 就特定技术问题提出重点问题,并提炼为简短的代码示例。 -
@TonyD 如果每次有人告诉我课程/学校/老师很愚蠢,我都能得到一毛钱。我会有很多 很多 的硬币。他们显然更好,但我在很多事情上别无选择。以后我会问一些小问题,谢谢。
标签: c++ floating-point integer dynamic-memory-allocation