Can't Access Properties From Object Stored In Vector - c++

Hey hey I'm a c++ newb here... i would appriciate someone pointing out an apparent misunderstanding i have about pointers and vectors (may be something else). I'm trying to create a switch element that handles several sub states
class MotionSwitch: public StateSwitch, public IMotion
{
public:
MotionSwitch(){ initialize(); };
~MotionSwitch(){};
//Make Motion States
MotionState restState; //Initialized In _states at 0
MotionState motionState;//" " 1
MotionState spinState;//" " 2
std::vector<MotionState*> _states;
};
class MotionState: public State, public IMotion
{
...
String msg = "Hello Is Motion";
...
};
The _states vector is initialized like this...
void MotionSwitch::initialize()
{
//Add States In Order Of ENUM (Because It Matters!)
_states.push_back(&restState);
_states.push_back(&motionState);
_states.push_back(&spinState);
}
The in following application code only one of the following will print
MotionState *currentState = widget._motionSwitch._states.at(0);
widget._com.log( "State Message...");
widget._com.log( currentState -> msg); //Won't Print
widget._com.log( widget._motionSwitch.restState.msg ); //This WILL Print
I would appreciate any help i can get here from you code gods!

Related

Why does the object my C++ point to lose its values in my factory pattern?

I'm trying to use a factory pattern to create different types of "State" objects. The objects are returned with a pointer (State*) but shortly after the objects are created, the values they point to disappear (go to NULL or reset to boolean "true").
The code directly below is where it goes awry, but below that is a complete code sample that compiles and runs. Additionally, I've posted pictures of the debugger values before and after the usleep() command.
I feel like it may have something to do with scope and the garbage collector, but I'm not a C++ expert by any stretch of the imagination. I would have thought my pointer would have kept my referenced object alive.
// relevant code
void execute(){
// Calling the constructor directly as an example
State directState = State("temp", false, false, false);
// Using factory pattern to create a state. Just creating the "default" state as an example
State * factoryState = StateFactory::getDefaultState();
// factoryState -> name is "Reading" in the debugger, but when I try to print it out, it's gone
// Grab the names for easy reference
const char * dName = directState.name;
const char * fName = factoryState -> name;
usleep(1000000 / 100);
// factoryState -> name .... it's vanished?
usleep(1000000 / 100);
// TODO we would run the factoryState -> execute() function here
}
// Complete code example
#include <iostream>
#include <zconf.h>
// Main generic "State" class
class State {
public:
const char * name;
bool isReadable;
bool isExecuting;
bool isFinished;
State(const char name[], bool isReadable, bool isExecuting, bool isFinished){
this -> name = name;
this -> isReadable = isReadable;
this -> isExecuting = isExecuting;
this -> isFinished = isFinished;
}
};
// An inherited class. There will be lots of these eventually
class StateReading: public State { ;
public:
StateReading():State((const char *)"Reading", true, false, false) {}
};
// Factory method that will create lots of the different states
// note that it will be returning a pointer to a "State" object
class StateFactory {
public:
static State* getDefaultState(){
StateReading defaultState = StateReading();
State* state = &defaultState;
return state;
}
};
// Runs the various "States" in a template pattern
class StateExecutor {
public:
State * state;
StateExecutor(){
StateReading stateReading = StateReading();
state = &stateReading;
}
void execute(){
// Calling the constructor directly as an example
State directState = State("temp", false, false, false);
// Using factory pattern to create a state. Just creating the "default" state as an example
State * factoryState = StateFactory::getDefaultState();
// factoryState -> name is "Reading" in the debugger, but when I try to print it out, it's gone
// Grab the names for easy reference
const char * dName = directState.name;
const char * fName = factoryState -> name;
usleep(1000000 / 100);
// factoryState -> name .... it's disappeard?
usleep(1000000 / 100);
// TODO we would run the factoryState -> execute() function here
}
};
// The actual
void loop(StateExecutor stateExecutor) {
// Run the "execute" function of whatever the current state is
// The stateExecutor actually runs the state
stateExecutor.execute();
// Slow the loop down a little. Just for effect
usleep(1000000 / 100);
}
// Simple program to recreate an event loop
int main() {
try {
StateExecutor stateExecutor = StateExecutor();
int count = 0;
do {
loop(stateExecutor);
count++;
// Arbitrarily break out of the loop after 100 events.
} while(count < 100);
} catch (std::exception& e){
std::cout << e.what() << '\n';
}
}
Here are the values directly after the factory created them. All looks good.
Gah! I called usleep() and the factoryState's name field is gone and the bools have reverted to true (cout does this as well). Black magic!
Here:
static State* getDefaultState(){
StateReading defaultState = StateReading();
State* state = &defaultState;
return state;
}
You return a pointer to defaultState. This state however is destroyed when the function returns. Using this pointer later is undefined behavior. You can declare defaultState as static, though i would rather make it a static member.

C++ member of class updated outside class

I have a question about pointers and references in C++. I am a programmer who normally programs in C# and PHP.
I have two classes (for now) which are the following.
The X/Y in Controller are continuously changing but i want them up to date in the Commands. I have multiple commands like Forward, Turn, Backward etc.
When i make the commands i give them the controller but the state (X, Y) of the controller are updating every second.
How can i fix that the controller attribute in the Commands are getting updated also every second?
class Forward : ICommand
{
Controller ctrl;
void Execute() {
int CurrentX = ctrl.X;
int CurrentY = ctrl.Y;
//Check here for the current location and calculate where he has to go.
}
}
class Controller
{
int X;
int Y;
void ExecuteCommand(ICommand command) {
command.Execute();
}
}
Main.cpp
Controller controller;
Forward cmd1 = new Forward(1, controller);
Turn cmd2 = new Turn(90, controller);
Forward cmd3 = new Forward(2, controller);
controller.Execute(cmd1);
controller.Execute(cmd2);
controller.Execute(cmd3);
I have read something about pointers and references and i think i have to use this but don't know how to use it in this situation.
(code can have some syntax errors but that's because i typed over. Everything is working further except for the updating).
If you use references rather than copy objects you can see changes.
#include <iostream>
using namespace std;
class ICommand
{
public:
virtual ~ICommand() = default;
virtual void Execute() = 0;
};
class Controller
{
public:
int X = 0;
int Y = 0;
void ExecuteCommand(ICommand & command) {
// ^-------
command.Execute();
}
};//,--- semicolons required
class Forward : public ICommand //note public
{
const int step;
Controller ctrlCopy;
Controller & ctrlReference;
public:
Forward(int step, Controller & ctrl) :
step(step),
ctrlCopy(ctrl), //this is a copy of an object
ctrlReference(ctrl) //this is a reference to an object
{
}
void Execute() {
std::cout << "copy: " << ctrlCopy.X << ", " << ctrlCopy.Y << '\n';
std::cout << " ref: " << ctrlReference.X << ", " << ctrlReference.Y << '\n';
//Check here for the current location and calculate where he has to go.
ctrlCopy.X += 10;
ctrlReference.X += 10;
}
};//<--- semicolons required
int main() {
Controller controller;
Forward cmd1(1, controller);
//Turn cmd2(90, controller); //Left for the OP to do
Forward cmd3(2, controller);
controller.ExecuteCommand(cmd1);
//controller.ExecuteCommand(cmd2);
controller.ExecuteCommand(cmd3);
//Do it again to show the copy and reference difference
std::cout << "Once more, with feeling\n";
controller.ExecuteCommand(cmd1);
controller.ExecuteCommand(cmd3);
}
Giving
copy: 0, 0
ref: 0, 0
copy: 0, 0 // [1]
ref: 10, 0 // [2]
Once more, with feeling
copy: 10, 0
ref: 20, 0
copy: 10, 0
ref: 30, 0
1 shows that the copy has X and Y of 0, while the reference shown in [2] has moved by the stated step (in controller.ExecuteCommand(cmd3))
Note, we don't need to use new to make this work (don't forget delete if you use new).
Also, void ExecuteCommand(ICommand command) now takes a reference instead otherwise the by-value copy does "slicing" (e.g. see here)

Cannot to delete the GameObject

I put the all GameObjects in scene under a list. the following is function used in the shelf.cs to delete the gameobjects. I assign the function to a button, so that it takes input value and find which Gameobject has lesser value than input. Then delete. The problem is whenever I click the button in the game preview, it won't delete the gameobjects. There are no warnings. I debug whether it receives all input, and it did. just not deleting the GameObjects.
Why?
public void Process(){
int user_apple,user_lemon,user_watermelon;
user_apple = int.Parse (input_apple.text);
user_lemon = int.Parse (input_lemon.text);
user_watermelon = int.Parse (input_watermelon.text);
Debug.Log (user_apple+" "+user_lemon+" "+user_watermelon);
for(int i = players.Count - 1; i >= 0; i--)
{ if(players[i].name == "Lemon")
{
if(players[i].GetComponent<Apple>().weight <= user_apple)
{ Debug.Log ("wat u want");
Destroy(players[i]);
}players.RemoveAt(i);
}
}
}
if i were to put it like this,
public void Process(){
int user_apple,user_lemon,user_watermelon;
user_apple = int.Parse (input_apple.text);
user_lemon = int.Parse (input_lemon.text);
user_watermelon = int.Parse (input_watermelon.text);
Debug.Log (user_apple+" "+user_lemon+" "+user_watermelon);
if(players[2].GetComponent<Lemon>().weight <= user_apple)
{ Destroy(players[2]);
players.RemoveAt(2);
}
}
it will have errors like below
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
Basket.Process () (at Assets/scripts/Basket.cs:77)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:110)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:575)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:717)
UnityEngine.Events.UnityEvent.Invoke () (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
This may not be the right solution try calling a co-routine from the Process Function and yield the loop so it runs everyframe.
Just to recap what Joe Blow said: there is no reason why this for loop should not work since it is totally independant of frames/time.
This little example works perfectly fine:
The script ScriptHolder is assigned to an empty gameobject (see screen below) and looks like this:
using UnityEngine;
using System.Collections.Generic;
public class ScriptHolder: MonoBehaviour
{
public List<GameObject> theGameObjects;
public void TheButtonFunction(int weight)
{
for(int i = theGameObjects.Count - 1; i >= 0; i--)
{
if(theGameObjects[i].GetComponent<TheObject>().objectWeight <= weight)
{
Destroy(theGameObjects[i]);
theGameObjects.RemoveAt(i);
}
}
}
}
All gameobjects were dragged into the public list.
The script for the gameobjects looks like this:
using UnityEngine;
using System.Collections;
public class TheObject : MonoBehaviour
{
public string objectName = "";
public int objectWeight = 0;
}
All GameObject_A are named A and have a weight of 10 (done in the inspector), GameObject_B are B with a weight of 15 and GameObject_C are C with a weight of 20. Columns are A to C from left to right.
The button calls the function from above with a value of 15.
When I click the button, the left and the middle columns of gameobjects are deleted from the scene and from the list - no problem whatsoever.

How to handle and avoid Recursions

I'm using custom classes to manage a vending machine. I can't figure out why it keeps throwing a stack overflow error. There are two versions to my program, the first is a basic test to see whether the classes etc work, by pre-defining certain variables. The second version is what it should be like, where the variables in question can change each time the program is ran (depending on user input).
If anyone can suggest ways of avoiding this recursion, or stack overflow, I'd great. Below is the code for the three classes involved;
class Filling
{
protected:
vector<Filling*> selection;
string fillingChosen;
public:
virtual float cost()
{
return 0;
}
virtual ~Filling(void)
{
//needs to be virtual in order to ensure Condiment destructor is called via Beverage pointer
}
};
class CondimentDecorator : public Filling
{
public:
Filling* filling;
void addToPancake(Filling* customerFilling)
{
filling = customerFilling;
}
~CondimentDecorator(void)
{
delete filling;
}
};
class Frosted : public CondimentDecorator
{
float cost()
{ //ERROR IS HERE//
return (.3 + filling->cost());
}
};
Below is the code used to call the above 'cost' function;
void displayCost(Filling* selectedFilling)
{
cout << selectedFilling->cost() << endl;
}
Below is part of the code that initiates it all (main method);
Filling* currentPancake = NULL;
bool invalid = true;
do
{
int selection = makeSelectionScreen(money, currentStock, thisState);
invalid = false;
if (selection == 1)
{
currentPancake = new ChocolateFilling;
}
else if...
.
.
.
.
else
invalid = true;
} while (invalid);
bool makingSelection = true;
CondimentDecorator* currentCondiment = NULL;
do
{
int coatingSelection = makeCoatingSelectionScreen(money, currentStock, thisState);
if (coatingSelection == 1)
currentCondiment = new Frosted;
else if (coatingSelection == 2)...
.
.
.
else if (coatingSelection == 0)
makingSelection = false;
currentCondiment = thisSelection;
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);
//Below is the code that DOES work, however it is merely meant to be a test. The
//above code is what is needed to work, however keeps causing stack overflows
//and I'm uncertain as to why one version works fine and the other doesn't
/*currentCondiment = new Frosted;
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);
currentCondiment = new Wildlicious;
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);*/
} while (makingSelection);
displayCost(currentPancake);
delete currentPancake;
The infinite recursion happens when you call displayCostwith a Frosted whose filling is a Frosted as well. And that happens right here:
currentCondiment->addToPancake(currentPancake);
currentPancake = currentCondiment;
displayCost(currentPancake);
You set the filling of currentCondiment to currentPancake, then call displayCost with currentCondiment.
In the process you also leak the memory that was originally assigned to currentPancake.
Btw currentCondiment = thisSelection; also leaks memory.
Idea: Use smart pointers like std::unique_ptr to get rid of the leaks.

How to send a message to the class that created the object

I would like to send back data to class that create this object.
It's game related.
The enemy objects have a threaded function and move on their own in the scene.
It generates lots of errors if you include the header file from the class that creates to the objects into the object itself ... to pass pointers.
Enemy Class:
Class Enemy
{
private:
void (*iChange)(DWORD &);
}:
Enemy::Enemy(void (*iChangeHandler)(DWORD &) ) : iChange(0)
{
this->iChange = iChangeHandler;
}
void Enemy::Draw(D3DGraphics& gfx)
{
this->iChange(this->dwThreadID); // send a message back to the class that created me
gfx.PutPixel(this->my_position_x + 0,this->my_position_y,this->red,this->blue,this->green);
this->grafix->DrawCircle(this->my_position_x + 0,this->my_position_y, this->radius, this->red,this->blue,this->green);
(sprintf)( this->enemy_buffer, "X: %d, Y: %d", this->my_position_x , this->my_position_y);
this->grafix->DrawString( this->enemy_buffer, this->my_position_x , this->my_position_y, &fixedSys, D3DCOLOR_XRGB(255, 0, 0) );
}
Game Class:
struct enemies_array_ARRAY {
std::string name;
DWORD ID;
Enemy* enemy;
} enemies_array[25];
void Game::EnemyEvent(DWORD &thread_id)
{
enemies_array[0]...... // i want to acces this struct array
}
Game::Game(HWND hWnd)
{
enemies_array[0].name = "john Doe";
enemies_array[0].ID = NULL;
enemies_array[0].enemy = new Enemy(&Game::EnemyEvent);
// error: C2664:
// another attemp
enemies_array[0].name = "john Doe";
enemies_array[0].ID = NULL;
enemies_array[0].enemy = new Enemy(Game::EnemyEvent);
// error C3867:
}
If I understand correctly, you want to call a function on the Game object. This means you need to pass a pointer to the Game object in order to correctly call a non static member function pointer(iChange) on it.
Make the changes shown below and you should be able to do what you want
enemies_array[0].enemy =  new Enemy(this,&Game::EnemyEvent);   
typedef void (Game::*ChangeFunc)(DWORD &)
Class Enemy
{
private:
ChangeFunc iChange;
Game *pGame;
}:
Enemy(Game *pCreatorGame, ChangeFunc iChangeHandler )
{
iChange = iChangeHandler;
pGame = pCreatorGame;
}
void Enemy::Draw(D3DGraphics& gfx)
{
(pGame->*iChange)(this->dwThreadID);