How do I initialize DefenseThread so that it will start executing Defend()? I have seen a lot of examples on how to do it from a .cpp file without a header, but non with.
void CommandDefend::DefendStart()
Gets the following error:
'&' requires I-value
Header:
#pragma once
#include <thread>
class CommandDefend : public ICommand
{
public:
CommandDefend();
~CommandDefend();
private:
thread* DefenseThread;
/// <summary>
/// Starts the commands execution.
/// </summary>
void DefendStart();
/// <summary>
/// Does the actual defending.
/// </summary>
void Defend();
};
CPP:
#include "stdafx.h"
#include "CommandDefend.h"
void CommandDefend::DefendStart()
{//ERROR HERE!!!!!!
DefenseThread = new thread(&CommandDefend::Defend);
}
void CommandDefend::Defend()
{
}
If you replace
DefenseThread = new thread(&CommandDefend::Defend);
with
DefenseThread = new thread(&CommandDefend::Defend, this);
it should work, because it's the way how I initialize class members of type std::thread (while they are not pointers).
Related
I am implementing a Vehicle-to-Vehicle communication in Veins. Two problematic files are CarAppLayer - Car application layer and TransmissionData - file responsible for the data transmission between the cars. For several days I am facing an error saying A runtime error occurred:
ASSERT: Condition 'mac' does not hold in function 'initialize' at veins/modules/application/ieee80211p/DemoBaseApplLayer.cc:49 -- in module (veins::TransmissionData) FLScenario.transmission_data (id=7), during network initialization
In the following, I made the code more readable by replacing unnecessary lines of code by ...
This is my Network file called FLScenario.ned
import org.car2x.veins.nodes.Scenario;
import TransmissionData;
network FLScenario extends Scenario
{
parameters:
#display("bgb=6665.92,5035.4717;bgi=background/terrain");
submodules:
transmission_data: TransmissionData; // If commented out, error disappears
}
The following three files, model the application layer of a car
CarAppLayer.ned
import org.car2x.veins.modules.application.ieee80211p.DemoBaseApplLayer;
import TransmissionData;
simple CarAppLayer extends DemoBaseApplLayer
{
parameters:
#class(veins::CarAppLayer);
#display("i=block/app2;p=1500,100");
}
CarAppLayer.h
#ifndef FL_CARAPPLAYER_H_
#define FL_CARAPPLAYER_H_
#include "veins/modules/application/ieee80211p/DemoBaseApplLayer.h"
#include "TransmissionData.h"
namespace veins {
class VEINS_API CarAppLayer : public DemoBaseApplLayer {
public:
void initialize(int stage) override;
...
protected:
TransmissionData* transmData;
...
protected:
void onWSM(BaseFrame1609_4* wsm) override;
void handleSelfMsg(cMessage* msg) override;
void handlePositionUpdate(cObject* obj) override;
private:
...
};
} // namespace veins
#endif /* FL_CARAPPLAYER_H_ */
CarAppLayer.cpp
...
#include "CarAppLayer.h"
using namespace veins;
Define_Module(veins::CarAppLayer);
void CarAppLayer::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
transmData = FindModule<TransmissionData*>::findGlobalModule(); // returns a pointer to TransmissionData object
ASSERT(transmData);
}
void CarAppLayer::handleSelfMsg(cMessage* msg)
{
TraCIDemo11pMessage* wsm = check_and_cast<TraCIDemo11pMessage*>(msg);
}
void CarAppLayer::onWSM(BaseFrame1609_4* frame)
{
}
void CarAppLayer::handlePositionUpdate(cObject* obj)
{
DemoBaseApplLayer::handlePositionUpdate(obj);
}
Last three files model the transmission of the packet between the cars
TransmissionData.ned
import org.car2x.veins.modules.application.ieee80211p.DemoBaseApplLayer;
simple TransmissionData extends DemoBaseApplLayer{
#class(veins::TransmissionData);
}
TransmissionData.h
#ifndef FL_TRANSMISSIONDATA_H_
#define FL_TRANSMISSIONDATA_H_
#include "veins/modules/application/ieee80211p/DemoBaseApplLayer.h"
namespace veins {
class VEINS_API TransmissionData : public DemoBaseApplLayer {
private:
...
public:
void initialize();
...
};
} //namespace veins
TransmissionData.cc
#include "TransmissionData.h"
namespace veins {
Define_Module(veins::TransmissionData);
void TransmissionData::initialize() {
EV << "TransmissionData, initialized \n";
}
} // namespace veins
DemoBaseApplLayer.cc mentioned in the error can be found here
Another observation is that the error also disappears if I run the program in Veins/examples directory and not in the separate project folder as I want to do. Could somebody help?
I am kind of new to C++ (and StackOverflow). I am trying to get something to work, but I have some #include problems.
I want to call a callback I made (from here), but I am struggling to do this.
This is my code so far. When I include child.hpp in the someclass.hpp file (because it needs information about Child for Callback<Child>), it has a looped include and the compiler crashes.
I have read about forward declarations (would be class Child; in the someclass.hpp file), and after trying I figured out this works, but I also read different opinions about this.
I have all .hpp files guarded with #ifndef CLASSNAME #define CLASSNAME ... #endif
Do I need to change my entire design, or what is the best option in my case?
base.hpp
#include "someclass.hpp"
class Base
{
protected:
unique_ptr<SomeClass> someClass;
};
base.cpp
#include "base.hpp"
Base::Base()
{
this->someClass = make_unique<SomeClass>();
}
child.hpp
#include "base.hpp"
class Child : public Base
{
public:
void callbackFunction(std::string data);
unique_ptr<Callback<Child>> callback;
};
child.cpp
#include "child.hpp"
void Child::callbackFunction(std::string data)
{
/*does something*/
}
Child::Child()
{
this->callback = make_unique<Callback<Child>>(this, &Child::callbackFunction);
//I can call this->callback->call(data); here without problems
this->someClass->setCallback(this->callback);
//^^^^^^^^^^^^^^^ == base.someClass
}
someclass.hpp
#include "child.hpp" // < does crash compiler due to loop
//> someclass.hpp uses child.hpp
//> child.hpp uses base.hpp
//> base.hpp uses someclass.hpp
// and thus loop
class SomeClass
{
public:
void someFunction(std::string data);
void setCallback(unique_ptr<Callback<Child>> callback);
unique_ptr<Callback<Child>> callbackInstance;
};
someclass.cpp
//not 100% sure about the type of this parameter
void setCallback(unique_ptr<Callback<Child>> callback)
{
this->callbackInstance = callback;
}
void SomeClass::someFunction(std::string data)
{
//here I want to call this "Child::callbackFunction" which should go like "this->callbackInstance->call(data)" ?
}
also in someclass.hpp
template<class T>
class Callback
{
public:
Callback(T* instance, void (T::*function)(std::string))
{
this->callbackInstance = instance;
this->callback = function;
}
void call(std::string data)
{
(callbackInstance->*callback)(data);
}
private:
T *callbackInstance;
void (T::*callback)(std::string);
};
To solve the mentioned error("expected class-name before '{' token on child.hpp") you should remove the #include "someclass.hpp" from base.hpp and replace it with a forward declaration for class SomeClass as shown below.
base.hpp
#ifndef BASE_H
#define BASE_H
//NO NEED TO INCLUDE someclass.hpp
#include <memory>
class SomeClass;//FORWARD DECLARE SomeClass
class Base
{
std::unique_ptr<SomeClass> someClass;
public:
//add declaration for default constructor
Base();
};
#endif
base.cpp
#include "base.hpp"
#include "someclass.hpp"
//other things here
Base::Base()
{
this->someClass = std::make_unique<SomeClass>();
}
child.hpp
#ifndef CHILD_H
#define CHILD_H
#include "base.hpp"
#include <memory>
#include "someclass.hpp"
class Child : public Base
{
public:
void callbackFunction(std::string data);
std::unique_ptr<Callback<Child>> callback;
//add declaration for default constrcutor
Child();
};
#endif
child.cpp
#include "child.hpp"
void Child::callbackFunction(std::string data){
/*does something*/
}
Child::Child()
{
this->callback = std::make_unique<Callback<Child>>(this, &Child::callbackFunction);
//I can call this->callback->call(data); here without problems
}
someclass.hpp
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include <string>
//REMOVED include child.hpp from here
class SomeClass
{
public:
void someFunction(std::string data);
//I think I need an instance of Callback<Child> here?
};
template<class T>
class Callback
{
public:
Callback(T* instance, void (T::*function)(std::string))
{
this->callbackInstance = instance;
this->callback = function;
}
void call(std::string data)
{
(callbackInstance->*callback)(data);
}
private:
T *callbackInstance;
void (T::*callback)(std::string);
};
#endif
someclass.cpp
#include "someclass.hpp"
void SomeClass::someFunction(std::string data)
{
//here I want to call this "Child::callbackFunction" which should go like "this->callbackInstance->call(data)" ?
}
The above program compiles and executes successfully as can be seen here.
Summary
Some of the changes that i made are listed below:
Removed unnecessary includes
Added declarations for default constructor in child.hpp and base.hpp
Added include guards in all headers.
I need to create a thread to run the Networking portion of my game. I would prefer to use SFML threads as my compiler doesn't yet support C++11 threads. However the class which contains the thread is created with make_shared(). Here is the code:
Game.cpp (not all the code just the declaration of GameScreen)
std::shared_ptr<Screen> Game::screen = std::make_shared<GameScreen>();
Screen is just a base class containing pure virtual functions. You should be able to figure out which ones are virtual based off the override keywords.
GameScreen.h
#ifndef GAMESCREEN_H
#define GAMESCREEN_H
#include <SFML/Graphics.hpp>
#include "Events.h"
#include "Screen.h"
#include "Map.h"
#include "Network.h"
class GameScreen : public Screen
{
public:
GameScreen();
void handleInput(sf::RenderWindow&) override;
void update(sf::RenderWindow&, sf::View&) override;
void render(sf::RenderWindow&) override;
private:
Map m_map;
Network network;
Events eventManager;
sf::Thread networkThread;
};
#endif // GAMESCREEN_H
GameScreen.cpp
#include <memory>
#include <iostream>
#include "GameScreen.h"
#include "Game.h"
GameScreen::GameScreen()
: networkThread(network.receive(eventManager))
{
network.Connect();
}
void GameScreen::handleInput(sf::RenderWindow& window)
{
/*Code*/
}
void GameScreen::update(sf::RenderWindow& window, sf::View& view)
{
/*Code*/
}
void GameScreen::render(sf::RenderWindow& window)
{
/*Code*/
}
Network.cpp (receive function only)
void Network::Recieve(Events& eManager)
{
sf::Packet m_rPacket;
m_socket.receive(m_rPacket, m_serverIP, port);
m_rPacket >> /*Data*/
eManager.addEvent(tmp);
}
You can use this in the constructor's initialization list:
MyClass::MyClass()
: AClass(&MyFunction(*this))
{
/*do stuff*/
}
However, this doesn't make sense in your example, because you are trying to pass a pointer to MyFunction (or its non-existent return value) to AClass(), and you can't quality a pointer with parameters. You can only pass parameters to MyFunction() when actually calling MyFunction(). Are you sure you don't actually mean something more like this instead:
MyClass::MyClass()
: AClass()
{
/*do stuff*/
MyFunction(*this);
}
Without seeing what AClass() actually is, or what it expects as input, it is difficult to know for sure what you are trying to do.
Update clearly you have not read the SFML documentation or SFML Tutorial on threading. The Thread constructor takes a pointer to a function/method as one input parameter, and an optional input value for the function/method as a separate parameter. Try this instead:
class MyClass : public sf::Thread
{
private:
static void MyFunction(MyClass &cls);
public:
MyClass();
};
MyClass::MyClass()
: sf::Thread(&MyClass::MyFunction, *this)
{
/*do stuff*/
}
void MyClass::MyFunction(MyClass &cls)
{
/*do stuff with 'cls'*/
}
Or this, as you can use a non-static class method with an SFML thread:
class MyClass : public sf::Thread
{
private:
void MyFunction();
public:
MyClass();
};
MyClass::MyClass()
: sf::Thread(&MyClass::MyFunction, *this)
{
/*do stuff*/
}
void MyClass::MyFunction()
{
/*do stuff with 'this'*/
}
Update: based on your new code, you are still not even close to constructing the sf::Thread object correctly (did you read the documentation/tutorial I linked to?). Also, your thread needs access to multiple objects owned by GameScreen, so you can't pass them all to the sf::Thread constructor. You need to do something more like this instead:
class GameScreen : public Screen
{
public:
GameScreen();
...
private:
...
Network network;
Events eventManager;
sf::Thread networkThread;
void networkThreadFunc();
};
GameScreen::GameScreen()
: networkThread(&GameScreen::networkThreadFunc, *this)
{
network.Connect();
}
void GameScreen::networkThreadFunc()
{
network.Receive(eventManager);
}
I'm a scholar for Computer Games Programming, currently studying C++. I'm trying to access a private Texture2D and Vector 2 type in a .h file from a .cpp file in order to give an object position and image.
This is the Player.cpp file
#include "Player.h"
#include <sstream>
Player::Player(int argc, char* argv[]) : Game(argc, argv), _cPlayerSpeed(0.1f), _cPlayerFrameTime(250)
{
//Player Inits
_playerDirection;
_playerFrame = 0;
_playerCurrentFrameTime = 0;
_playerSpeedMultiplier = 1.0f;
//Init of Important Game Aspects
Graphics::Initialise(argc, argv, this, 1024, 768, false, 25, 25, "Genocide: Remastered", 60);
Input::Initialise();
Graphics::StartGameLoop(); //Start of Game Loop, calls Update and Draw in game loop.
}
Player::~Player()
{
}
void Player::Input(int elapsedTime, Input::KeyboardState* state)
{
// Checks for directional keys pressed
if (state->IsKeyDown(Input::Keys::D))
{
_playerPosition->X += _cPlayerSpeed * elapsedTime;
}
}
/// <summary> All content should be loaded in this method. </summary>
void Player::LoadContent()
{
_playerPosition = new Vector2();
_playerTexture = new Texture2D();
_playerTexture->Load(" ", false);
_playerSourceRect = new Rect(0.0f, 0.0f, 0, 0);
}
/// <summary> Called every frame - update game logic here. </summary>
void Player::Update(int elapsedTime)
{
}
/// <summary> Called every frame - draw game here. </summary>
void Player::Draw(int elapsedTime)
{
}
This is the Player.h
#pragma once
#ifdef WIN32
#ifndef _DEBUG
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif
#endif
#include "S2D/S2D.h"
using namespace S2D;
class Player : public Game
{
public:
Player(int argc, char* argv[]);
~Player();
/// <summary> All content should be loaded in this method. </summary>
void virtual LoadContent();
/// <summary> Called every frame - update game logic here. </summary>
void virtual Update(int elapsedTime);
/// <summary> Called every frame - draw game here. </summary>
void virtual Draw(int elapsedTime);
private:
Vector2* _playerPostion;
Rect* _playerSourceRect;
Texture2D* _pacmanTexture;
const float _cPlayerSpeed;
const int _cPlayerFrameTime;
int _playerDirection;
int _playerFrame;
int _playerCurrentFrameTime;
float _playerSpeedMultiplier;
void Input(int elapsedTime, Input::KeyboardState* state);
void CheckPaused(Input::KeyboardState* state, Input::Keys pauseKey);
void CheckViewportCollision();
void UpdatePlayer();
};
I've literally copied and pasted something I've been working on with my Lecturer and changed variable, type and instantiation declaration and his works. Curious as to why mine isn't. Help will be much appreciated.
Many thanks,
Ryan.
In the header, your Texture2D* is called _pacmanTexture, whereas in your implementation, you've called it _playerTexture. Similarly, you've misspelled _playerPosition in the header.
Usual way to give access to private resources of a class to another class is to add public accessor methods (getter, setter).
I have two classes: AbstractClass and SubClass.
This is basically my code (well, just some example code):
abstractclass.h
class AbstractClass
{
public:
AbstractClass();
void doSomething();
protected:
virtual void implementMe() = 0;
int a;
};
abstractclass.cpp
#include "abstractclass.h"
AbstractClass::AbstractClass(){}
void AbstractClass::doSomething()
{
implementMe(); // compiler error: "implementMe() was not declared in this scope"
a = 0; // same compiler error here...
}
subclass.h
#include "abstractclass.h"
class SubClass : public AbstractClass
{
public:
SubClass();
protected:
void implementMe();
};
subclass.cpp
#include "subclass.h"
SubClass::SubClass() {}
void SubClass::implementMe()
{
// do some stuff
}
In the AbstractClass, however, I keep getting a compiler error (for the virtual function as well as for the class variable):
implementMe() was not declared in this scope
The only way I found to get rid of this was to use forward-declaration:
void implementMe();
AbstractClass::doSomething()
{
implementMe();
}
I cannot believe that this is the correct way, though?
Thanks!
EDIT:
Ok, as my conceptual understanding of subclassing in C++ doesn't seem to be totally wrong (see the comments), I'm gonna share some of my original source code. Hopefully this will help to indentify the error.
This is my abstract / base class:
abstractenvironment.h
#ifndef ABSTRACTENVIRONMENT_H
#define ABSTRACTENVIRONMENT_H
#include <QObject>
class AbstractEnvironment : public QObject
{
Q_OBJECT
public:
AbstractEnvironment(QObject *parent = 0);
protected:
virtual void process() = 0;
quint32 counter;
private slots:
void handleTimeout();
};
#endif // ABSTRACTENVIRONMENT_H
abstractenvironment.cpp
#include "abstractenvironment.h"
#include <QTimer>
QTimer *myTimer;
AbstractEnvironment::AbstractEnvironment(QObject *parent) :
QObject(parent)
{
myTimer = new QTimer(this);
connect(myTimer, &QTimer::timeout, this, &AbstractEnvironment::handleTimeout);
myTimer->start(1);
counter = 0;
}
void handleTimeout()
{
process();
counter++;
}
And this is my subclass:
environment.h
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H
#include "abstractenvironment.h"
class Environment : public AbstractEnvironment
{
Q_OBJECT
public:
Environment(Controller *controller, QObject *parent = 0);
protected:
void process();
};
#endif // ENVIRONMENT_H
environment.cpp
#include "environment.h"
Environment::Environment(Controller *controller, QObject *parent) :
AbstractEnvironment(controller, parent) {}
void Environment::process()
{
// do something
}
PS: I've learned from the first part of this question and tried to compile the source code above inside Qt with MinGW. I get exactly two error messages (as expected):
..\untitled\abstractenvironment.cpp: In function 'void handleTimeout()':
..\untitled\abstractenvironment.cpp:17:13: error: 'process' was not declared in this scope
..\untitled\abstractenvironment.cpp:18:5: error: 'counter' was not declared in this scope
In case you want to try it yourself, I've zipped the Qt project and uploaded it to my Dropbox (of course I will remove this file at some point but the code is exactly the same as in the post above --> it's just for the sake of convenience, so you don't have to copy-paste it yourself)
EDIT: You just changed your question. So I can't tell if your original text was your actual source code or not. Good rule of thumb, paste your actual code rather than paraphrase it (then de-identify or reduce it if needed).
ORIGINAL ANSWER:
implementMe(); // compiler error: "implementMe() was not declared in this scope"
That is because doSomething() isn't declared properly in AbstractClass. You "declared" it in the base class with:
doSomething();
The compiler doesn't recognize AbstractClass::doSomething() out of line definition so nothing inside the implementation is resolved to the class scope.
Change that to:
void doSomething();
just like in your derived class.
and
AbstractClass::doSomething()
{
implementMe();
}
to
void AbstractClass::doSomething()
{
implementMe();
}
UPDATE:
void handleTimeout()
{
process();
counter++;
}
is a global function. That isn't the class implementation. It should be:
void AbstractClass::handleTimeout()
{
process();
counter++;
}
In abstractenvironment.cpp you define void handleTimeout(), which is non-member function and does not relate to AbstractEnvironment class. Thus, it doesn't look for AbstractEnvironment::process() and AbstractEnvironment::counter, but for ::process() and ::counter instead (which are not declared, hence the error).
Change it to void AbstractEnvironment::handleTimeout() and it should compile.