简体   繁体   English

我不明白我的错误,我需要返回一个指针数组

[英]I don't understand my errors where I need to return an array of pointers

The question that I tried answering is: 我尝试回答的问题是:

Design a class called client_base. 设计一个名为client_base的类。 A client_base has a maximum capacity of clients it can manage and a list of all the clients in its current database. 一个client_base具有它可以管理的最大客户端容量,以及当前数据库中所有客户端的列表。 We can add clients to the client base as long as it hasn't reached its capacity. 我们可以将客户添加到客户群中,只要它尚未达到其容量即可。

Your client base class must provide the following public methods: 您的客户基础类必须提供以下公共方法:

client_base();
    client_base(int capacity);

    int get_total_client_count();
    int get_client_count(int category);
    client * get_current_client_list();
    bool add_client(client new_client);

I already completed the code, but there is one important line that I need to include that has some error which is segmentation fault (core dumped) so I have commented it out. 我已经完成了代码,但是有一条重要的代码行需要我添加,它有一些错误,即段错误(核心转储),因此我已将其注释掉。 It runs without the line, but this line allows for the code to add a new client to the count. 它运行时没有一行,但是此行允许代码将新客户端添加到计数中。

I have three separate files that compile together. 我有三个单独的文件一起编译。

client_base.h FILE: client_base.h文件:

#include <iostream>
#include "client.h"

#ifndef CLIENT_BASE
#define CLIENT_BASE

class client_base
{
    public:

    client_base();
    client_base(int capacity);

    int get_total_client_count();
    int get_client_count(int category);
    client * get_current_client_list();
    bool add_client(client new_client);

    int count;
    int cap;
    client* list;

    ~client_base();
};

#endif

client_base.cpp FILE: client_base.cpp文件:

#include <iostream>
#include "client.h"
#include "client_base.h"
using namespace std;

client_base::client_base()
{

}

client_base::client_base(int capacity)
{
    cap = capacity;
    count = 0;
    for (int i = 0; i < cap; i++){
        //cout << "list[" << i << "] = " << list + i << endl;
    }
}

int client_base::get_total_client_count()
{
    return count;
}

int client_base::get_client_count(int category)
{
    int answer = 0;
    for(int i = 0; i < count; i++)
    {
        if (list[count].get_category() == category)
        {
            answer = answer + 1;
        }
    }
    return answer;
}

client * client_base::get_current_client_list()
{
    return list;
}

bool client_base::add_client(client new_client)
{
    if(count < cap)
    {
        //list + count = new_client;
        count++;
        cout << "adding new client" << endl;
        return true;
    }
    cout << "full, not adding new client" << endl;
    return false;
}

client_base::~client_base()
{

}

main-1-2.cpp FILE: main-1-2.cpp文件:

#include <iostream>
#include "client.h"
#include "client_base.h"
using namespace std;

int main()
{
    client_base base(3);
    client obj1("A", 1);
    client obj2("B", 2);
    client obj3("C", 2);
    client obj4("D", 5);
    bool add;

    add = base.add_client(obj1);
    cout << "total count = " << base.get_total_client_count() << endl;
    add = base.add_client(obj2);
    cout << "total count = " << base.get_total_client_count() << endl;
    add = base.add_client(obj3);
    cout << "total count = " << base.get_total_client_count() << endl;
    add = base.add_client(obj4);
    cout << "total count = " << base.get_total_client_count() << endl;

    return 0;
}

I don't understand the output as well... 我也不明白输出...

Just an addition to the code, the code for client.h file is: 只是对代码的补充,client.h文件的代码是:

#include <iostream>

#ifndef CLIENT
#define CLIENT

class client
{
    public:
        client();
        client(std::string client_id, int category);

        std::string get_id();
        int get_category();
        void set_id(std::string id);
        void set_category(int category);


        std::string id;
        int cate;        
};

#endif

Try like so: 尝试这样:

client_base::client_base(int capacity)
{
    count = capacity;
    cap = sizeof(client) * count; // change declaration of cap from int to size_t
    list = (client*) new client[count]; // allocate the pointer list before use 
    for (int i = 0; i < count; i++){
        cout << "list[" << i << "] = " << (list + i)->get_id() << endl; // print content of the pointer
    }
}


client_base::~client_base()
{
    delete list; // delete pointer in the destructor to avoid memory leak
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM