I have a vector of shared pointers of a pure virtual class IObserver (observers), the vector is a class which inherits IObserver. There is also a second class which inherits IObserver, HomeOwner. I want to cast Homeowner objects to the observers vector, so I can send them to the functions in SecurityStatus but not sure how I go about it. I'd like to do it behind the scenes rather than in the main program. The declaration for both is below.
Thanks!
#pragma once
#include "IObserver.h"
#include "HouseSecurity.h"
#include "SensorInfo.h"
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
// concrete observer
class HomeOwner : public IObserver{
SensorInfo currentSensorState;
string myName;
string myPhoneNumber;
string myEmail;
HouseSecurity &house;
public:
void Update();
void New();
HomeOwner(string, string, string);
};
#pragma once
#include "IObserver.h"
#include "HomeOwner.h"
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
#include <memory>
using namespace std;
// subject
class SecurityStatus{
vector <shared_ptr<IObserver>> observers;
public:
void AttachObserver(shared_ptr<IObserver>);
void DetachObserver(shared_ptr<IObserver>);
void NotifyObservers();
};
Related
I have to do polymorphism for the go() function but i cant figure out how to access private variable distance. The complier keeps showing that
‘int Transport::distance’ is private within this context
5 | distance = 100;
Here is my code
Transport.h
#ifndef TRANSPORT_H
#define TRANSPORT_H
#include <iostream>
using namespace std;
class Transport{
private:
int distance;
public:
int get_dist_travelled();
virtual void go();
};
#endif
Transport.cpp
#include "Transport.h"
int Transport::get_dist_travelled(){
return distance;
}
Horse.h
#ifndef HORSE_H
#define HORSE_H
#include <iostream>
#include "Transport.h"
using namespace std;
class Horse:public Transport{
public:
void go();
};
#endif
Horse.cpp
#include "Horse.h"
#include "Transport.h"
void Horse::go() {
distance = 100;
}
main.cpp
#include <iostream>
#include "Transport.h"
#include "Horse.h"
using namespace std;
int main(){
Horse kid;
kid.go();
cout<<kid.get_dist_travelled()<<endl;
}
You can on the one hand make the variable protected, or define the class as a friend class.
I have a template class called item.
I have another class having array of items - class hashTable.
I have third class which derived from hashTable - HSubject.
I'm confused about the using/include that I have to add to each class.
I tried many options but it produced run time errors.
code is attached here:
*item.h*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
enum state { empty, full, deleted };
template <class T, class K>
class item
{
// propeties and functions here
};
*hashTable.h*
#pragma once
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include "item.h"
using namespace std;
// tried adding this but didn't seem to work
//template <class T, class K>
//class item<T, K>;
template <class T, class K>
class hashTable
{
public:
vector<item<T, K>> hashT;
hashTable();
// propeties and functions here
};
*hashTable.cpp*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
#include "hashTable.h"
using namespace std;
template<class T, class K>
hashTable<T, K>::hashTable()
{}
// functions are written here
*HSubject.h*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
class item<list<string>, string>;
using namespace std;
class HSubject :
public hashTable<list<string>, string>
{
public:
HSubject();
// propeties and functions here
};
*HSubject.cpp*
#pragma once
#include "stdafx.h"
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <memory>
#include "hashTable.h"
#include "HSubject.h"
#include "item.h"
using namespace std;
HSubject::HSubject() : hashTable()
{
}
// functions are written here
Any help would be greatful. Thanks!
This is problem #2 from this previous question:
Inheritance in Arduino Code
Building off of Steven's answer, I do need the array that holds the pointers to persist outside of its scope, which is resulting in some weird behavior.
This is my "Board" class I have so far, that contains multiple child elements:
Board.h:
#ifndef Board_h
#define Board_h
#include <StandardCplusplus.h>
#include <serstream>
#include <string>
#include <vector>
#include <iterator>
#include "Arduino.h"
#include "Marble.h"
#include "Wall.h"
class Board
{
public:
Board();
void draw(double* matrix);
private:
Marble marble;
//std::vector<Actor> children;
Actor* children[2];
};
#endif
Board.cpp:
#include "Arduino.h"
#include "Board.h"
#include <math.h>
#include <iterator>
#include <vector>
Board::Board()
{
}
void Board::create(double* _matrix, int _cols, int _rows) {
Marble *marble = new Marble();
Wall wall;
children[0] = marble;
//children.push_back(marble);
//children.push_back(wall);
}
void Board::draw(double* matrix) {
Serial.println("board draw");
children[0]->speak();
}
In my "loop" function I am calling
board.draw(matrix);
which results in some nutty Serial code being written out.
Clearly I am not understanding the ins and outs of pointers in arrays in classes here.
You need to make Actor::speak virtual, the compiler uses dynamic binding for virtual methods.
class Actor
{
public:
Actor();
virtual void speak(); // virtual
private:
};
Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".
What is going on?
#include "MyClass.h"
class MyOtherClass {
public:
MyOtherClass();
~MyOtherClass();
MyClass myVar; //Unknown type Error
};
Suddenly when I include the .h and write that var Xcode gives me tons of errors... and also the unknown type error.
How can it be unknown when the .h is included right there?
Here is the NodeButton.h file which would correspond to the MyClass.h in the example
#pragma once
#include "cinder/Vector.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Color.h"
#include "cinder/ImageIo.h"
#include "cinder/Timeline.h"
#include "cinder/app/AppBasic.h"
#include "cinder/App/App.h"
#include "Node.h"
#include "CursorMano.h"
using namespace ci;
using namespace ci::app;
using namespace std;
using namespace is;
typedef boost::shared_ptr<class NodeButton> NodeButtonRef;
class NodeButton : public Node2D
{
public:
NodeButton (CursorMano *cursor, string imageUrl, bool fadeIn = false, float delay = 0.0f);
virtual ~NodeButton ();
//methods
void update( double elapsed );
void draw();
void setup();
//events
bool mouseMove( ci::app::MouseEvent event );
//vars
CursorMano *mCursor;
gl::Texture mImageTexture;
Anim<float> mAlpha = 1.0f;
bool mSelected = false;
private:
};
And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.
#pragma once
#include <list>
#include <vector>
#include "cinder/app/AppBasic.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"
#include "NodeButton.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CursorMano {
public:
CursorMano (AppBasic *app);
~CursorMano ();
void mueveMano(Vec2i);
void update();
void draw();
void play(int button);
void reset(int button);
Vec2i mMousePos;
NodeButton mButtonCaller; //this gives the unknow type error
private:
AppBasic *mApp;
gl::Texture mFrameTexture;
qtime::MovieGl mMovie;
int mIdButton;
};
You have a circular dependency of your header files.
NodeButton.h defines NodeButton class which CursorMano.h needs to include so that compiler can see definition for NodeButton but NodeButton.h itself includes CursorMano.h.
You will need to use forward declarations to break this circular dependency.
In NodeButton.h you just use an pointer to CursorMano so You do not need to include the CursorMano.h just forward declare the class after the using namespace declarations.
using namespace std;
using namespace is;
class CursorMano;
It's probably a result of the circular dependency between you two header files (NodeButton includes CursorMano and CursorMano includes NodeButton). Try removing the #include "CursorMano.h" in NodeButton.h and add class CursorMano; before your NodeButton declaration.