John Tedesco
John Tedesco

Reputation: 55

How to store object class data in a database class array

I am trying to build a database class to store information for a player class such as their name, year of birth, etc. I have the player class completed but I am having a hard time trying to implement the data into the database class. In this class I am creating 2 functions, one to add a player to the database, and one to search for a player. The code looks like this:

#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




};

I just can't figure out how to place the addPlayer data into the array made in Player players_. Again thanks for any help, I greatly appreciate it and sorry for the messy code I am still fairly new to c++ and I am trying to get better. Thanks.

Upvotes: 0

Views: 62

Answers (1)

Beta
Beta

Reputation: 99084

You are attempting something beyond your ability, and I respect that, but in this case it's such a reach that you can't learn anything.

When you write software, start with something very simple that works perfectly, then add complexity a little at a time.

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

  int getThing(int k)
  {
    return(things[k]);
  }
private:
  int things[5];
};

Once you have that working perfectly, you can either adapt it to do more sophisticated tasks like keep track of the number of things, add a thing and search for a thing, or you can start using a container like std::vector which already has those functions.

Once you have that working perfectly, you can work on your Player class. If you want to put Players in a container, they must behave like inert cargo, which for now means that they must be assignable and copyable. They probably are, but at some point you'll have to learn assignment operators and copy constructors.

Assuming they are well-behaved, you can adapt your database class with methods like this:

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

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

Remember: start small and simple, build up slowly, test at every step, and never add to code that doesn't work.

Upvotes: 2

Related Questions