[英]I don't understand my errors where I need to return an array of pointers
我尝试回答的问题是:
设计一个名为client_base的类。 一个client_base具有它可以管理的最大客户端容量,以及当前数据库中所有客户端的列表。 我们可以将客户添加到客户群中,只要它尚未达到其容量即可。
您的客户基础类必须提供以下公共方法:
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);
我已经完成了代码,但是有一条重要的代码行需要我添加,它有一些错误,即段错误(核心转储),因此我已将其注释掉。 它运行时没有一行,但是此行允许代码将新客户端添加到计数中。
我有三个单独的文件一起编译。
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文件:
#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文件:
#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;
}
我也不明白输出...
只是对代码的补充,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
尝试这样:
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.