I'm trying to make a class with an array in it, but I'm having trouble with declaring an array between a header file and the .cpp, can someone tell me how to write that or what I need to change?
header "players.h";
1 2 3 4 5 6 7 8 9 10 11 12 13
#pragma once
#include "base.h";
class players: public base
{
private:
char* itemstats;
char* modmove[4];
public:
players(void);
~players(void);
};
Eventually the modmove array and itemstats variable will be linked to player moves and held-items that will affect player's stats.
I just started this code because I'm trying to learn inheritance, but the array just got me held up, so I don't really want to continue with it until I figure out to code an array between header and .cpp. Thank you.
P.S. this is not homework, I'm just learning C++ on my spare time.
// cpp
players::players() : modmove(4) // initialize modmove with four empty strings
// itemstats is default initialized to an empty string,
{
// ...
}
Thanks JLBorges, I haven't used vectors yet, mostly because my book hasn't covered them yet and I didn't want to jump ahead, but I'll take a look at the tutorials.
I found a work-around where I could declare each member in my array individually or through a while loop, but vectors would probably be the way to go, I kind of have an idea of how they work.
vectors are good but its important to understand arrays too - I would suggest something like this
players.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "players.h"
players::players(void)
{
itemstats = "null";
for (int i = 0; i < 4; ++i)
modmove[i] = "null";
}
players::~players(void)
{
}
players.h
1 2 3 4 5 6 7 8 9 10 11 12 13
#pragma once
#include "base.h";
class players : public base
{
private:
char* itemstats;
char* modmove[4];
public:
players(void);
~players(void);
};
Basically the idea here is you cannot value-initialize an array like you had - you have to step through the array and give each element the value you want - this is easily accomplished with a for loop
Certainly yes. Especially when you are just starting out with C++
>> but its important to understand arrays too
Eventually, yes.
However, understanding the intricacies of arrays is not of central importance right now; learning programming is. Arrays can wait. Just use std::vector<>.