简体   繁体   中英

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. A client_base has a maximum capacity of clients it can manage and a list of all the clients in its current database. 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:

#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:

#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:

#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:

#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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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