I have 2 simple C++ headers implemented as in the following:
Attribute.h
#include <string>
using namespace std;
class IAttribute
{
virtual string getName(){};
};
class StringAttribute : public IAttribute
{
private:
string name = "";
string value = "";
public:
StringAttribute(string name, string value)
{
this->name = name;
this->value = value;
}
string getName()
{
return this->name;
}
string getStrValue()
{
return value;
}
void setValue(string value)
{
this->value = value;
}
};
tableRow.h
#include "attribute.h"
#include <vector>
using namespace std;
class TableRow
{
private:
vector<IAttribute *> attributeList;
int rowId;
public:
TableRow(int rowId)
{
this->rowId = rowId;
}
void addStrAttribute(string name, string value)
{
attributeList.push_back(new StringAttribute(name, value));
}
StringAttribute getStrAtt(string name)
{
for (int i = 0; i < (int)attributeList.size(); i++)
{
if (attributeList[i]->)//couldn't access the methods of StringAttributeImp
{
}
}
}
};
As in the comment of tableRow header above, I couldn't access the methods and properties of the Implementation class. What is wrong?
The getName function is private in the IAttribute class. So of course you're not able to access it.
You should change the getName function to public; or use friend class.
Related
I am making a class called "StateMachine" to make other classes that inherit that logic.
Trying to make the "State" structure store a pointer to the function to execute in that state, I have found myself needing to use lambda functions. However, I can't seem to declare an auto member inside a struct without it being "scatic const".
statemachine.h
#include <vector>
#include <string>
#include <map>
using namespace std;
struct State
{
string id;
auto action;
string next;
State():id(""){}
State(string _id, auto func, string n = ""):id(_id), action(func), next(n){}
};
class StateMachine
{
protected:
void addState(string id, auto func)
{
if(activeState == ""){activeState = id;}
stateList.insert(pair<string, State>(id, State(id, func)));
}
void doAction()
{
if(stateList[activeState].action != nullptr)
{
(*stateList[activeState].action)();
if(stateList[activeState].next != "")
{
activeState = stateList[activeState].next;
}
}
}
void setState(string state)
{
if(stateList.count(state) != 0)
{
activeState = state;
}
}
string getState()
{
return activeState;
}
private:
map<string, State> stateList;
string activeState = "";
};
Example of use
class Player : public StateMachine
{
public:
Player()
{
addState("ST_SPAWN", [this]{this->OnSpawn();});
}
virtual ~Player(){}
void OnSpawn()
{
//...
}
};
The error
/home/yawin/Dokumentuak/Proyectos/C++/Dough/src/./include/interfaces/statemachine.h:34:10: error: non-static data member declared ‘auto’
auto action;
What can I do?
You could use std::function to simplify this.
#include <functional>
struct State {
string id;
std::function<void()> action;
string next;
State(){id = "";}
State(string _id, std::function<void()> func, string n = "") :
id(_id), action(func), next(n) {}
};
class StateMachine {
//...
void addState(string id, std::function<void()> func) {
if(activeState == "") activeState = id;
stateList.emplace(id, State{id, func});
}
//...
I have a class that has a property named title implemented as a nested class, how can I call std::strings's methods on it ? (I am trying to make hidden encapsulation.)
#include <iostream>
class test {
public:
class {
private :
std::string value;
public:
operator const std::string & () {
return value;
}
void operator =(const std::string & source) { value = source;}
} title;
};
int main() {
test t;
t.title = "title"; // this works.
t.title.find("t"); // error here.
}
What's wrong with this code (I minimized my whole code)? I can't figure out why pu.useIt(); causes a segmentation fault.
#include <memory>
using namespace std;
class Person {
private:
shared_ptr<string> name;
public:
void setName(shared_ptr<string> name) {
this->name = name;
}
shared_ptr<string> getName() {
return name;
}
};
class PersonCreator {
shared_ptr<Person> person;
public:
void createAmy() {
shared_ptr<string> amysName = make_shared<string>("amy");
person->setName(amysName);
}
};
class PersonUser {
public:
void useIt() {
PersonCreator pc;
pc.createAmy();
}
};
int main()
{
PersonUser pu;
pu.useIt();
return 0;
}
You need to initialize person, now it is empty, default ctor of shared_ptr means that it points to nullptr:
void createAmy() {
shared_ptr<string> amysName = make_shared<string>("amy");
person = std::make_shared<Person>(); // added
person->setName(amysName);
}
I'm doing an assignment for class and I'm getting weird compile errors.
The errors are saying error: 'Noble' has not been declared and error: class 'Warrior' does not have any field named 'pBoss'.
I'm new to C++ so I really don't get what I'm doing wrong here - it seems like I declared both classes and all the member fields.
#include <vector>
#include <iostream>
#include <ostream>
using namespace std;
class Warrior {
public:
Warrior(string name, double str) : name(name), strength(str), pBoss(nullptr) {}
bool hire(Noble* noble) { }
bool fire(Noble* noble) { }
void lost() { }
void won(double damageRatio) { }
string getName() const { return name; }
double getStrength() const { return strength; }
Noble* getBoss() const { return pBoss; }
void display(ostream& os = cout) const { }
private:
string name;
double strength;
Noble* pBoss;
};
class Noble {
public:
Noble(string name) : name(name), alive(true) {}
bool hire(Warrior& war) { }
bool fire(Warrior& war) { }
bool battle(Noble& enemy) { }
void display(ostream& os = cout) const { }
private:
vector<Warrior*> army;
string name;
double strength;
bool alive;
void lost() { }
void won(double damage) { }
};
I took out the function descriptions so this won't be too long, but let me know if there is anything I didn't include and I'll update the post!
By the time you define Warrior, Noble is not yet defined. C++ works line by line. To fix this you could forward declare the classes.
// forward declaration
class Warrior;
class Noble;
class Warrior {
// ...
};
class Noble {
// ...
};
Warrior has no idea what a Noble is. You need to forward declare Noble. Place this before the Warrior declaration:
class Noble;
class Warrior { ...
Furthermore, you've misspelled pBoss in the Warrior constructor, change the constructor to:
Warrior(string name, double str) : name(name), strength(str), pBoss(nullptr) {}
Lastly you have an extra close brace floating around in Noble after the display declaration.
Looks like you may have an extra brace in your noble class:
class Noble {
public:
Noble(string name) : name(name), alive(true) {}
bool hire(Warrior& war) { }
bool fire(Warrior& war) { }
bool battle(Noble& enemy) { }
void display(ostream& os = cout) const { }
} // <-- out of place?
private:
vector<Warrior*> army;
string name;
double strength;
bool alive;
void lost() { }
void won(double damage) { }
};
Why does it produce a segmentation fault when I create a Rectangle object? I'm thinking that there is something about my constructors that is incorrect but I don't have enough experience in C++ to identify what.
#include <string>
#include <map>
using namespace std;
class Shape {
private:
string name;
string property_name;
map<string,double> parameters;
public:
Shape(){
}
void set_name(string n){
name=n;
}
string set_property_name(string s){
property_name=s;
}
void set_parameter(string p, double n){
parameters[p]=n;
}
double get_parameter(string p) {
return parameters[p];
}
virtual double get_property() = 0;
};
class Shape2D: public Shape {
public:
Shape2D() {
set_property_name("area");
}
};
class Rectangle: public Shape2D {
public:
Rectangle() {
set_name("rectangle");
set_parameter("length",0);
set_parameter("base",0);
}
double get_property() {
return get_parameter("length") * get_parameter("base");
}
};
int main() {
Shape * user_shape;
user_shape=new Rectangle();
return 0;
}
Because your string set_property_name(string s) have no return and is undefined behavior