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;
}
I have a class called Person:
class Person {
private:
char name[50];
unsigned int number;
public:
void set_data(const char *newname, unsigned int number) {
*name= *newname; //THE MAIN PROBLEM I WANT TO SOLVE
this->number = number;
}
char* get_name() {
return this->name;
}
unsigned int get_number() {
return this->number;
}
};
I have arrays:
const char *names[] = {"Mark", "Pavel", "Bill", "Jasur", "Jeff"};
int phones[] = { 1234567890, 9876543210, 123321456654, 1998946848479, 1234554321 };
I'm using for loop to set Person members:
Person p;
for (int i = 0; i < 5; i++) {
p.set_data(names[i], phones[i]);
cout << p.get_name() << endl;
}
When I run this, I'm getting wrong output.
Using * the way you are, you are only copying the 1st char into name. You are also not null-terminating name either, which is required by the overloaded operator<< that takes a char* as input.
To copy the entire string, you need to use std::strcpy() (or better, std::strncpy()) instead, eg:
#include <cstring>
void set_data(const char *newname, unsigned int newnumber) {
//std::strcpy(name, newname);
std::strncpy(name, newname, 49);
name[49] = '\0';
number = newnumber;
}
However, since this is C++ and not C, you really should be using std::string instead:
#include <string>
class Person {
private:
std::string name;
unsigned int number;
public:
void set_data(const std::string &newname, unsigned int newnumber) {
name = newname;
number = newnumber;
}
std::string get_name() const {
return name;
}
/* or:
const std::string& get_name() const {
return name;
}
*/
unsigned int get_number() const {
return number;
}
};
I'm trying to create a class for employees, and have a problem with its constructor.
My class looks like that (please note the name parameter which is char* type):
class Employee {
int id;
char * name ;
float salary;
int hours;
int extra;
public:
//constructor
Employee(int, char *, float, int, int);
//Getters and Setters:
void setId(int a) { id = a; }
int getId() { return id; }
void setName(char * c) { name = c; }
char * getName() { return name; }
void setSalary(float f) { salary = f; }
float getSalary() { return salary; }
void setHours(int h) { hours = h; }
int getHours() { return hours; }
void setExtra(int e) { extra = e; }
int getExtra() { return extra; }
};
I built a constructor and I want it to have default parameters, and I don't know how to deal with the name parameter to have a default of let's say "unknown".
This is the constructor:
Employee::Employee(int i = 123456789, char * na, float sal = 30, int ho = 45, int ex = 10)
{
id = i;
name = na;
salary = sal;
hours = ho;
extra = ex;
}
You can use a character array, and initialise the array to point to the first element of the array. You can store the array for example as a static member:
// in the class definition
inline static char default_name[] = "unknown";
// in argument list of the constructor
... char * na = default_name, ...
Although, you may want to consider whether it makes sense for name to be pointer to non-const. Perhaps a pointer to const would suffice. In such case, you could initialise it to point to the literal "unknown" directly.
A cleaner version
class Employee {
int id = 0;
char *name = nullptr;
float salary = 0.0;
int hours = 0;
int extra = 0;
And you don't need to have constructors, this depends on the case, but you get the idea that by initializing the variables on the definition you reduce the inconsistency of having multiples constructors for example
I have the following struct:
const int max_stu= 100;
const int max_name =100;
const int max_add=100;
struct stu{
int id;
char name[max_name];
char addr[max_add];
};
stu student[max_stu];
One of my friends already give me this code
struct stu{
int id;
char name[max_name];
char addr[max_add];
void remove(stu temp){
temp.id= 0;
temp.name= "";
temp.addr= "";
}
for (int i=0;i < max_stu; i++){
stu.remove(student[i]);
}
but it have problem in this part
temp.name= "";
because of the type of char and
max_name=100
and on this part
stu.remove(student[i]);
because its not a class. How can I fix this?
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()));