Using sets as properties that are initialized with the class - c++

I have been trying to use sets as properties for a class. These sets need to be initialized when the class is initialized. I have been trying to use pre-initialized arrays and then set the values of the sets to the contents of the arrays. My problem with this, however, is that it seems like you can only set the contents of a set to the contents of an array when the set is declared which I can't do if it's a class property. I would rather not individually add each piece of the set. Any recommendations? Here's some sample code:
Creature.cpp:
Creature :: Creature()
{
Skill Init[] = {Balance,EscapeArtist,Hide,MoveSilently,OpenLock,Ride,SleightOfHand,Tumble,UseRope};
//DexSkills = ??? (contents of init)
Skill Init[] = {Climb,Jump,Swim};
//StrSkills = ???
Skill Init[] = {Concentration};
//ConSkills = ???
Skill Init[] = {Appraise,Craft,DecipherScript,DisableDevice,Forgery,Knowledge,Psicraft,Search,Spellcraft};
//IntSkills = ???
Skill Init[] = {Autohypnosis,ControlShape,Heal,Listen,Profession,SenseMotive,Spot,Survival};
//WisSkills = ???
Skill Init[] = {Bluff,Diplomacy,Disguise,GatherInformation,HandleAnimal,Intimidate,Perform,UseMagicDevice,UsePsionicDevice};
//ChaSkills = ???
}
and in Creature.h:
#pragma once
#define roll20 (rand()%20) + 1
#define Mod(stat) ((stat-10)/2)
#include <vector>
#include <set>
#include "Global.h"
class Creature
{
protected:
std::set<Skill> DexSkills;
std::set<Skill> StrSkills;
std::set<Skill> ConSkills;
std::set<Skill> IntSkills;
std::set<Skill> WisSkills;
std::set<Skill> ChaSkills;
//static const Skill StrSkills[3];
//static const Skill ConSkills[1];
//static const Skill IntSkills[9];
//static const Skill WisSkills[8];
//static const Skill ChaSkills[9];
const int maxHP;
int HP;
int hitDie;
int speed;
int babBase;
int fortBase;
int refBase;
int willBase;
int AC;
int STR;
int CON;
int DEX;
int INT;
int WIS;
int CHA;
DamageResist DR;
std::vector<Feat> Feats;
Alignment alignment;
//std::set<DamageType> Weaknesses;
//std::set<DamageType> Resistances;
std::set<DamageType> Immunities;
int Resistances [15];
int SkillRanks[40];
public:
//Creature();
int Damage(int damage, DamageType type);
void getDamaged(int damage);
int StatRoll(int stat);
//int StrRoll();
//int ConRoll();
//int DexRoll();
//int IntRoll();
//int WisRoll();
//int ChaRoll();
int SkillCheck(Skill skill);
};
and in my global.h:
typedef enum {
Appraise,Autohypnosis,Balance,Bluff,Climb,Concentration,ControlShape,Craft,DecipherScript,Diplomacy,DisableDevice,Disguise,EscapeArtist,Forgery,
GatherInformation,HandleAnimal,Heal,Hide,Intimidate,Jump,Knowledge,Listen,MoveSilently,OpenLock,Perform,Profession,Psicraft,Ride,Search,SenseMotive,
SleightOfHand,SpeakLanguage,Spellcraft,Spot,Survival,Swim,Tumble,UseMagicDevice,UsePsionicDevice,UseRope
} Skill;

If you are looking for a simple way to copy a C array into a set, you can try something like this:
#include <iterator>
...
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
std::set<int> s;
std::copy(a, a + n, std::inserter(s, s.end()));

Related

how to initialize an array of objects in a constructor in c ++

I try to put an array of objects as a argument,
I know that you can initialize them separately with
constructorName (className array [], double arg2): name1 (arg1), name2(arg2){...}
But I can't do that with an array of objects, is there any other way to do this?
code example:
#include <iostream>
#include <string>
using namespace std;
class Atlet{
private:
string name;
int number;
string nacionality;
double time;
public:
Atlet(string n, int num, string nacio, double t){
name = n;
number = num;
nacionality = nacio;
time = t;
}
string getName(){
return name;
}
};
class Race{
private:
double distance;
Atlet runners[];
public:
//how to initalize arrays of objects here .
Race( Atlet arr[], double dist): runners(arr), distance(dist){
runners = arr;
distance = dist;
}
};
test.cpp program.
#include <iostream>
#include "bible.cpp"
using namespace std;
int main(){
Atlet t1 = Atlet("Usaimbol",12,"Madagascar", 10.21);
Atlet t2 = Atlet("Juan",15,"USA", 10.54);
Atlet comps[] = {t1, t2};
Race race1 = Race(comps, 120.00);
return 0;
}
I try to make this, but:
I want to say that Race( Atlet arr[], double dist): runners(arr), distance(dist){ should just be Race( Atlet arr[], double dist) { (taking out the colon stuff inline) but I cannot be certain.

How do I define and set a a pointer to a 2d array that's passed twice in the middle class

I have an array in my MainProcessor
float waveforms[2][1080] = { {0}, {0} };
And I pass it to a SynthVoice like this:
osc1Voice->setWavetable(waveforms, 0); //[1]
osc2Voice->setWavetable(waveforms, 1);
There I read it like this in SynthVoice.h:
typedef float array_of_wavetable[1080];
void setWavetable(array_of_wavetable* waveform, int number)
{
wavetable = waveform;
id = number;
}
so i can use it like this in my SynthVoice class:
wavetable[id][first_sample + int(phase_0)]
Now I have moved [1] in a new class (OscilatorProcessor.h) and I want to set a pointer from MainProcessor -> OscillatorProcessor -> SynthVoice in a way that I can use it as wavetable[id][phase].
Now I don't know how to pass it from my MainProcessor to my OscillatorProcessor in the middle. So I can read it in the same way in the SynthVoice class.
I hope this made sense. Thank you for your time
edit: minimal reproducible example
#include <iostream>
typedef float array_of_wavetable[1080];
class SynthVoice
{
public:
void setWavetable(array_of_wavetable* waveform, int number)
{
wavetable = waveform;
id = number;
}
void outputWavetable()
{
for(int i = 0; i < 1080; i++)
{
std::cout << wavetable[id][i];
}
}
private:
array_of_wavetable * wavetable;
int id;
};
class OscillatorProcessor
{
public:
void setWavetable(array_of_wavetable * waveform, int number)
{
wavetable = waveform;
id = number;
}
void sendWaveToVoice()
{
SynthVoice voice;
voice.setWavetable(wavetable, id);
voice.outputWavetable();
}
private:
array_of_wavetable * wavetable;
int id;
};
int main() {
float waveform_templates[4][1080] = { {0}, {0}, {0}, {0} };
OscillatorProcessor oscillator;
oscillator.setWavetable(waveform_templates, 2);
return 0;
}

How to switch to a dynamic array?

I'm trying to set the size of an array to an input received by the program, currently the size of the array is defined as a const static int.
Board.h
#include <iostream>
using namespace std;
class Board {
private:
const static int BOARDSIZE = 5;
char board[BOARDSIZE][BOARDSIZE];
const char p1Symbol = 'R';
const char p2Symbol = 'B';
const char trail = 'O';
const char crash = '*';
int p1Row;
int p1Col;
int p2Row;
int p2Col;
public:
void setBoardSize(int size);
bool isValidMove(int row, int col);
bool isValidInput(char input);
bool getNextMove();
void initializeArray();
void drawHorizontalSeparator();
void drawSeparatedValues(int row);
void displayBoard();
};
Main
#include <iostream>
#include "Board.h"
using namespace std;
int main() {
int size;
cout << "Enter and integer between 4 and 20 for the BoardSize: " << endl;
cin >> size;
Board b;
b.setBoardSize(size);
b.initializeArray();
b.displayBoard();
bool done = false;
while (!done) {
done = b.getNextMove();
}
return 0;
}
I'm trying to use this function called setBoardSize to change the size of the board in the Board.H file. I've tried removing const static but then I get all sorts of errors. Apparently it isn't possible to define an array that doesn't have a predefined size?
Instead of char board[BOARDSIZE][BOARDSIZE] go for std::vector<char>(BOARDSIZE * BOARDSIZE, 0) and access elements as vector[y * BOARDSIZE + x] where x and y run within <0, BOARDSIZE).
You would resize simply as vector.resize(size * size).

Nested Structures declaration and how to access them

I am having trouble using a pointer to a nested structure and using those variables in those structures in my member functions of my class.
I have these variables declared. Let me know if they are declared right in relation to the nesting of the structures.
const int MAX_ACCOUNT_COUNTRY = 36;
const int MAX_ACCOUNT_CITY = 50;
const int MAX_ACCOUNT_NAME = 10;
const int MAX_NUMBER = 16;
const int MAX_ACCOUNT_ADDRESS = 25;
const int MAX_ACCOUNT_EMAIL = 50;
const int MAX_OFFICE_HOURS = 20;
const int businessID = 0;
const int residentalID = 1;
typedef char accountCountry[MAX_ACCOUNT_COUNTRY + 1];
typedef char accountCity[MAX_ACCOUNT_CITY + 1];
typedef char accountName[MAX_ACCOUNT_NAME + 1];
typedef char phoneNumberFormat[MAX_NUMBER + 1];
typedef char accountAddress[MAX_ACCOUNT_ADDRESS + 1];
typedef char accountEmail[MAX_ACCOUNT_EMAIL + 1];
typedef char officeHours[MAX_OFFICE_HOURS + 1];
typedef phoneBook * phonebookPtr;
public:
Phonebook(const int numPages);
~Phonebook();
void addAccount(const int);
void removeAccount(const int);
void editAccount(const int);
void viewPhonebook();
private:
struct phoneBook{
struct businessAccountEntry
{
const int businessID;
accountCountry businessCountry;
accountCity businessCity;
accountName businessName;
accountName accountHolder;
accountAddress businessAddress;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
phoneNumberFormat faxNumber;
accountEmail businessEmail;
officeHours businessOfficeHours;
};
struct residentialAccountEntry
{
const int residentialID;
accountName residentName;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
};
};
phonebookPtr thePhonebook;
int counterID = 0;
Phonebook();
Phonebook(Phonebook &);
};
How do I access the variables inside the structure businessAccountEntry in my class for my member functions?
Give them a name like
struct phoneBook{
struct businessAccountEntry
{
const int businessID;
accountCountry businessCountry;
accountCity businessCity;
accountName businessName;
accountName accountHolder;
accountAddress businessAddress;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
phoneNumberFormat faxNumber;
accountEmail businessEmail;
officeHours businessOfficeHours;
} businessAccount;
struct residentialAccountEntry
{
const int residentialID;
accountName residentName;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
} residentialAccount;
};
and use it : thePhonebook->residentialAccount.residentialID

matrix in a class in C++

the following code is for three classes , class One, class Two, class Three.
class Three takes tow vectors, the first vector contains instances of One , the second vector contains instances of Two.
I want to get a 2D matrix via a method in Three , this matrix will have two equal indices each one is the size of the vector of One instances.
I don't know where to declare this matrix , and how to initialize it.
i will present a code is working fine before i declare the matrix , then i will present one example of my many tries which is not working and producing error messages.
here is the code before declaring the matrix(it works fine)
#include<iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
const unsigned int N = 5;
class One
{
private:
unsigned int id;
public:
unsigned int get_id(){return id;};
void set_id(unsigned int value) {id = value;};
One(unsigned int init_val = 0): id(init_val) {}; // constructor
~One() {}; // destructor
};
class Two
{
private:
One first_one;
One second_one;
unsigned int rank;
public:
unsigned int get_rank() {return rank;};
void set_rank(unsigned int value) {rank = value;};
unsigned int get_One_1(){return first_one.get_id();};
unsigned int get_One_2(){return second_one.get_id();};
Two(const One& One_1 = 0, const One& One_2 =0 , unsigned int init_rank = 0)
: first_one(One_1), second_one(One_2), rank(init_rank)
{
}
~Two() {} ; // destructor
};
class Three
{
private:
std::vector<One> ones;
std::vector<Two> twos;
public:
Three(vector<One>& one_vector, vector<Two>& two_vector)
: ones(one_vector), twos(two_vector)
{
}
~Three() {};
vector<One> get_ones(){return ones;};
vector<Two> get_twos(){return twos;};
void set_ones(vector<One> vector_1_value) {ones = vector_1_value;};
void set_twos(vector<Two> vector_2_value) {twos = vector_2_value;};
};
int main()
{
cout<< "Hello, This is a draft for classes"<< endl;
vector<One> elements(5);
cout<<elements[1].get_id()<<endl;
vector<Two> members(10);
cout<<members[8].get_One_1()<<endl;
Three item(elements, members);
cout<<item.get_ones()[3].get_id() << endl;
return 0;
}
now i declared a method to produce a matrix in Three , the method's name is get_Mat() here is the code :
#include<iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
const unsigned int N = 5;
class One
{
private:
unsigned int id;
public:
unsigned int get_id(){return id;};
void set_id(unsigned int value) {id = value;};
One(unsigned int init_val = 0): id(init_val) {}; // constructor
~One() {}; // destructor
};
class Two
{
private:
One first_one;
One second_one;
unsigned int rank;
public:
unsigned int get_rank() {return rank;};
void set_rank(unsigned int value) {rank = value;};
unsigned int get_One_1(){return first_one.get_id();};
unsigned int get_One_2(){return second_one.get_id();};
Two(const One& One_1 = 0, const One& One_2 =0 , unsigned int init_rank = 0)
: first_one(One_1), second_one(One_2), rank(init_rank)
{
}
~Two() {} ; // destructor
};
class Three
{
private:
std::vector<One> ones;
std::vector<Two> twos;
public:
Three(vector<One>& one_vector, vector<Two>& two_vector)
: ones(one_vector), twos(two_vector)
{
}
~Three() {};
vector<One> get_ones(){return ones;};
vector<Two> get_twos(){return twos;};
void set_ones(vector<One> vector_1_value) {ones = vector_1_value;};
void set_twos(vector<Two> vector_2_value) {twos = vector_2_value;};
unsigned int get_Mat() {
unsigned int mat[ones.size()][ones.size()];
for(unsigned int i = 0; i < ones.size(); ++i)
for(unsigned int j = 0; j < ones.size(); ++j)
mat[i][j] = 1;
return mat;}
};
int main()
{
cout<< "Hello, This is a draft for classes"<< endl;
vector<One> elements(5);
cout<<elements[1].get_id()<<endl;
vector<Two> members(10);
cout<<members[8].get_One_1()<<endl;
Three item(elements, members);
cout<<item.get_ones()[3].get_id() << endl;
return 0;
}
I will be very thankful if you can help me to find a way to produce this matrix via a method in class Three.
Thanks.
get_Mat returns an integer, not a matrix. It is better to use vector<vector<unsigned int> >, that will avoid a lot of troubles later on.
Or have a look here (c++):
Return a 2d array from a function
or here (C):
Return a 2d array from a function