将类对象传递给列表表
Passing a class object to a list table
我有一个结构列表,一个类castomer
.我想将castomer
存储到列表中。为此,我每次都会创建一个castomer
,并将它们发送到列表表。除了每次我尝试将castomer
存储到表中时程序崩溃之外,没有任何错误。我有这个列表和类:
class castomer{
private:
string name;
string lastname;
int number;
double time;
public:
void setAll(string,string,int,double);
int numberR(){return number;}
double timeR(){return time;}
void displayAll();
};
struct node {
castomer person;
struct node *next;
};
此函数用于添加castomer
:
void add(clock_t *start,struct node *table[])
{
*start = clock();
double time=(double)*start;
int i=0;
while(table[i]!=NULL)
{
i++;
}
if(i > 24)
cout << "We are not able to add another castomer becase we are full please wait for ont to go "<<endl;
else{
castomer c1;
cout<<i;
cout<< "Give me the castomers name and lastname :";
string temp1,temp2;
cin>>temp1;
cin>>temp2;
c1.setAll(temp1,temp2,i,time);
table[i]->person=c1;//my program crases here anyone knows why?
}
}
ps:在我的主要table[]
看起来像这样struct node * table[25];
struct node* table[25]
这是将表声明为包含 25 个指针的数组。您必须为查看代码时缺少的每个指针分配内存。
在你的 while-loop 之后,table[i]
将被NULL
。
table[i]->person = c1; // Your program crashes here because of that.
按原样使用您的代码,您应该这样做
table[i] = new node;
table[i]->person = c1;
但是代码看起来很奇怪,就像你想实现一个链表(node
结构),但由于某种原因坚持使用数组。
如果你的目标是一个链表,你需要重新考虑你的很多代码。
如果不是,则可能会完全丢失node
类型。
既然你正在使用(或尝试使用)C++我将提到一些你可以做的事情来改进你的代码,让你自己的生活更轻松。
在C++中,您可以定义用于初始化对象的构造函数。
您的函数setAll
是一种不好的做法。您真的不想在创建后更改一个人的所有数据。您只想在创建时初始化数据。很好地使用构造函数。
你不需要指针
对于你想要做的事情,你不需要使用指针使代码复杂化,你可以通过引用传递参数。
您正在使用C++,请使用 STL
具体矢量我保证,会帮助你。
使用cout <<
显示对象
您可以向类中添加friend ostream& operator<<
函数,以便能够编写如下代码:
Customer a;
cout << a << endl;
一个完整的例子:
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
using namespace std;
class Customer
{
public:
Customer(){} // default constructor.
Customer(string pname, string plastname): name(pname), lastname(plastname)
{
id = ++idgen; // Set an id number.
time = (double)clock(); // initialize time.
}
int getId() {return id;} // former numberR().
double getTime() {return time;} // former timeR().
friend ostream& operator<<(ostream &out, Customer obj)
{out << obj.name << " " << obj.lastname << ". " << "Id: " << obj.id << " Time: " << obj.time;}
private:
static int idgen; // static values are a good way for keep some sort of id.
int id; // former member: number.
double time; // Why don't use clock_t directly?
string name, lastname;
};
int Customer::idgen = 0; // Initialize static variable.
int main()
{
int const MAX_NUMBER_PERSONS = 2;
std::vector<Customer> customer_list;
string name, lastname;
while (customer_list.size() < MAX_NUMBER_PERSONS)
{
cout << "Give me the castomers name and lastname <name> <lastname>:";
cin >> name >> lastname;
customer_list.push_back(Customer(name, lastname));
}
for (auto &x: customer_list) // If you're learnign C++ its a good moment for search
{ // for c++11 doc.
cout << x << endl;
}
return 0;
}
相关文章:
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 从封装在对象中的函数 C++ 返回时为空的列表
- 如何在不销毁对象的情况下返回对象列表
- 如何使用Q_PROPERTY公开自定义对象列表
- 链接列表在 cpp 中包含不同的对象类
- 如何使用对象制作分数列表并获得平均值
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 我可以从列表中获取对象并复制它们,但如何删除我复制的对象?
- 如何获取列表的每个对象并调用getName方法来打印其名称
- 如何在Windows Media Foundation SDK中指定语言列表对象
- 在 DLL 中嵌入 Python:Py_DECREF列表对象时访问冲突读取位置
- 将类的每个实例添加到列表对象的构造函数
- 列表<对象*>迭代器中的分段错误
- 如何使用list::sort()对STL列表对象进行排序,同时将自定义排序函数作为参数传递给list::排序
- 如何防止客户端修改列表对象
- c++列表对象错误
- 在列表对象中使用对象的派生函数
- 编译器错误 使用列表<对象*的迭代器> C++