如何在对象中正确使用整数和浮点
How do I use integers and floats within an object correctly?
因此,我正试图编写一个程序来跟踪具有以下属性的葡萄酒厂的各种属性。
-动态分配内存
-整合者追踪酒厂启动的年份
-成功率和葡萄园占地面积的浮动。
-添加、删除、显示(按评级和名称)和保存酒庄数据的功能。
我已经编写/调试了代码,以使用类似的程序,其中所有属性都是cstrings,并且此代码基于相同的框架。
我的问题是,适用于动态分配的cstrings的相同代码不适用于整数或浮点,并且我从编译器中收到了转换错误消息。我有一个包含aWinery
的list
类,由于我使用私有成员来隐藏数据,所以我有一些函数要从客户端程序传递到list
和aWinery
。具有相同类型的函数是相同的,所以我在这里只包括每种类型中的一种。
有人能指出我做错了什么吗?如果aWinery
对int和float的定义不是指针,我不知道如何通过参数返回。我能在类中找到的动态内存编程的唯一例子是使用cstrings。我在代码中注意到了几点,这是我从一位讲师那里复制的,因为我无法理解。
EDIT:我得到的具体错误是invalid conversion from int* to intn 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
我认为你的大部分问题都在标题中,这里是:
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编写,使用动态分配的字符数组来保存字符串可能是一种必要的邪恶,但在C++中这只是受虐狂)
通过按值存储所有数据,您可以消除所有new
和delete
命令(以及随之而来的不可避免的错误和效率低下),并在必要时直接设置和获取值。
此外,此访问器功能错误:
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
}
- C++使用整数的压缩数组初始化对象
- 为对象分配整数.输出将是什么?
- 获取我的基类以递增派生类对象整数
- C++对象(整数)
- 如何声明包含整数数组的对象
- 静态强制转换允许转换对象指针,但不允许转换整数
- 如何使用 c++ 模板实现整数、字符串、浮点数和日期对象的数组
- 具有整数成员的对象的 printf
- 如何在不使用对象的情况下连接字符串、整数和浮点数
- 如何使用字符串流对象多次将整数转换为字符串
- 如何在具有指向对象的指针数组的类中创建复制构造函数和析构函数,其中对象本身具有指向整数的指针数组
- C++插入到地图<双精度、多地图<整数、对象*>>
- 如何在对象中正确使用整数和浮点
- 运算符重载 - 将对象与整数进行比较
- C++:从字符串对象中提取整数
- C++宏整数和对象定义
- C++ - 使用 stringstream 对象从外部 txt 文件中的句子中读取字符串和整数
- 使用构造函数c++将一个长整数转换为类对象
- c++中带整数和对象的操作符重载
- 大规模处理锯齿化整数对象