将类对象传递给列表表

Passing a class object to a list table

本文关键字:列表 对象      更新时间:2023-10-16

我有一个结构列表,一个类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;
}