C++ 如何在数据库类数组中存储对象类数据

C++ 如何在数据库类数组中存储对象类数据,c++,C++,我试图构建一个数据库类来存储玩家类的信息,比如他们的姓名、出生年份等。我已经完成了玩家类,但是我很难将数据实现到数据库类中。在这个类中,我创建了两个函数,一个用于向数据库添加播放器,另一个用于搜索播放器。代码如下所示: #include <iostream> #include <string> #include "player.h" using namespace std; const int kMaxPlayers = 100; class playerDB { p

我试图构建一个数据库类来存储玩家类的信息,比如他们的姓名、出生年份等。我已经完成了玩家类,但是我很难将数据实现到数据库类中。在这个类中,我创建了两个函数,一个用于向数据库添加播放器,另一个用于搜索播放器。代码如下所示:

#include <iostream>
#include <string>
#include "player.h"

using namespace std;
const int kMaxPlayers = 100;
class playerDB {

public:

  playerDB() = default;
  ~playerDB() = default;
//adds player to the Database
Player& AddPlayer(string in_first_name, string in_last_name, string in_team_name, string in_goals, string in_assists){
  Player& = in_first_name, in_last_name, in_team_name, in_goals, in_assists;
//I don't know if this is correct it isn't compiling as it is.
}
//allows one to find a player based on their last name
Player& GetPlayer(string in_last_name){
  return(in_last_name);
  // this is still temporary, I just have it here to let it compile

}
private:
  Player players_[kMaxPlayers];
  //stores the individual record in the array
  int next_slot_;
  //tracks the next space in the array to place a new record




};
void addPlayer(Player &newPlayer)
{...}

void addPlayer(string in_first_name, string in_last_name)
{
  Player newP(in_first_name, in_last_name);
  addPlayer(newP);
}
#包括
#包括
#包括“player.h”
使用名称空间std;
常数int kMaxPlayers=100;
班长{
公众:
playerDB()=默认值;
~playerDB()=默认值;
//将播放器添加到数据库中
Player&AddPlayer(名字串、姓氏串、球队名串、进球串、助攻串){
球员&=in_first_name、in_last_name、in_team_name、in_进球、in_助攻;
//我不知道这是不是正确,它不是编译。
}
//允许根据玩家的姓氏查找玩家
Player&GetPlayer(姓氏中的字符串){
报税表(姓氏);
//这仍然是暂时的,我只是在这里让它编译
}
私人:
玩家玩家[kMaxPlayers];
//将单个记录存储在数组中
int next_slot_uu;
//跟踪阵列中的下一个空间以放置新记录
};

我只是不知道如何将addPlayer数据放入Player中生成的数组中。再次感谢任何帮助,我非常感激和抱歉,混乱的代码,我还是相当新的C++,我正在努力变得更好。谢谢。

您正在尝试一些超出您能力范围的事情,我尊重这一点,但在这种情况下,您无法学到任何东西

当你编写软件时,从一个非常简单、工作完美的东西开始,然后一次增加一点复杂性

class firstDB {
public:
  void assignThing(int k, int t)
  {
    things[k] = t;
  }

  int getThing(int k)
  {
    return(things[k]);
  }
private:
  int things[5];
};
一旦你能很好地工作,你可以调整它来完成更复杂的任务,比如跟踪事物的数量,添加一个事物并搜索一个事物,或者你可以开始使用一个容器,比如
std::vector
,它已经有了这些功能

一旦你能完美地工作,你就可以在你的
播放器上工作了。如果你想把玩家放在一个容器中,他们的行为必须像惰性货物一样,这就意味着他们必须是可分配和可复制的。它们可能是,但在某个时候,您必须学习赋值运算符和复制构造函数

假设它们表现良好,您可以使用如下方法调整数据库类:

#include <iostream>
#include <string>
#include "player.h"

using namespace std;
const int kMaxPlayers = 100;
class playerDB {

public:

  playerDB() = default;
  ~playerDB() = default;
//adds player to the Database
Player& AddPlayer(string in_first_name, string in_last_name, string in_team_name, string in_goals, string in_assists){
  Player& = in_first_name, in_last_name, in_team_name, in_goals, in_assists;
//I don't know if this is correct it isn't compiling as it is.
}
//allows one to find a player based on their last name
Player& GetPlayer(string in_last_name){
  return(in_last_name);
  // this is still temporary, I just have it here to let it compile

}
private:
  Player players_[kMaxPlayers];
  //stores the individual record in the array
  int next_slot_;
  //tracks the next space in the array to place a new record




};
void addPlayer(Player &newPlayer)
{...}

void addPlayer(string in_first_name, string in_last_name)
{
  Player newP(in_first_name, in_last_name);
  addPlayer(newP);
}

记住:从小处开始,慢慢地建立,每一步都要测试,千万不要添加不起作用的代码。

你正在尝试一些超出你能力范围的东西,我尊重这一点,但在这种情况下,你无法学到任何东西

当你编写软件时,从一个非常简单、工作完美的东西开始,然后一次增加一点复杂性

class firstDB {
public:
  void assignThing(int k, int t)
  {
    things[k] = t;
  }

  int getThing(int k)
  {
    return(things[k]);
  }
private:
  int things[5];
};
一旦你能很好地工作,你可以调整它来完成更复杂的任务,比如跟踪事物的数量,添加一个事物并搜索一个事物,或者你可以开始使用一个容器,比如
std::vector
,它已经有了这些功能

一旦你能完美地工作,你就可以在你的
播放器上工作了。如果你想把玩家放在一个容器中,他们的行为必须像惰性货物一样,这就意味着他们必须是可分配和可复制的。它们可能是,但在某个时候,您必须学习赋值运算符和复制构造函数

假设它们表现良好,您可以使用如下方法调整数据库类:

#include <iostream>
#include <string>
#include "player.h"

using namespace std;
const int kMaxPlayers = 100;
class playerDB {

public:

  playerDB() = default;
  ~playerDB() = default;
//adds player to the Database
Player& AddPlayer(string in_first_name, string in_last_name, string in_team_name, string in_goals, string in_assists){
  Player& = in_first_name, in_last_name, in_team_name, in_goals, in_assists;
//I don't know if this is correct it isn't compiling as it is.
}
//allows one to find a player based on their last name
Player& GetPlayer(string in_last_name){
  return(in_last_name);
  // this is still temporary, I just have it here to let it compile

}
private:
  Player players_[kMaxPlayers];
  //stores the individual record in the array
  int next_slot_;
  //tracks the next space in the array to place a new record




};
void addPlayer(Player &newPlayer)
{...}

void addPlayer(string in_first_name, string in_last_name)
{
  Player newP(in_first_name, in_last_name);
  addPlayer(newP);
}

请记住:从小处开始,慢慢构建,每一步都进行测试,永远不要添加不起作用的代码。

是否确实要使用数组,而不是像标准> > STD:向量< /代码>的标准容器?我如何使用STD::向量?如果使用效率更高,更好的使用,我宁愿使用C++学习的源代码?@ JordeDeCo适当使用和许多其他类,将是一个适当的C++课程(文本,类,两者)的一部分,而不是这个站点的真正主题。也就是说,它可能只是标准库中最常用的模板类iostream库和std::string库的后面。研究它确实对你有好处。你确定你想要使用一个数组,而不是一个标准容器,比如“代码> STD::向量< /代码>?我如何使用STD::vector?如果使用它更有效和更好,我宁愿使用C++学习的源代码?”将是一个适当的C++课程的一部分(文本,类,两者),而不是真正的主题这个网站。也就是说,它可能只是标准库中最常用的模板类iostream库和std::string库的后面。研究它确实会对你很有帮助。谢谢你,这真的帮了大忙。我想我确实需要开始慢一点。再次感谢-约翰谢谢你这真的帮了大忙。我想我确实需要开始慢一点。再次感谢你-约翰