Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This is header file name monitor.h
class DSRAgent;
class Detector;
class ReputationSystem;
class Monitor {
public:
Monitor();
Monitor(DSRAgent* agent);
void handleTap(const Packet* packet);
void handlePublishInfo(nsaddr_t src, double identification, int count,rating* ratings);
void handlePacketSent(Packet* packet);
void publishInfo(map<nsaddr_t, rating*> ratings);
void setNetID(nsaddr_t netid);
nsaddr_t getNetID();
friend class PackTableTimer;
void setReputationSystem(ReputationSystem* rep_system);
bool isPACK(int uid);
void Terminate();
PackTableTimer* pack_table_timer;
private:
void packTableCheck();
map<nsaddr_t, double> published_ids;
PackTable pack_t;
DSRAgent* dsragent;
Detector* detector;
ReputationSystem* reputation_system;
nsaddr_t net_id;
};
class PackTableTimer : public TimerHandler {
public:
PackTableTimer(Monitor *a) : TimerHandler() { a_ = a;}
void expire(Event *e);
protected:
Monitor *a_;
};
the other file that is monitor.cc
void PackTableTimer::expire(Event *e)
{
a_->packTableCheck();
resched(PACK_TIMEOUT + PACK_TIMEOUT * Random::uniform(1.0));
}
DSRAgent* dsragent;
Monitor::Monitor(DSRAgent* agent)
{
this->dsragent = agent;
this->detector = new Detector(this);
pack_table_timer = new PackTableTimer(this);
pack_table_timer->sched(PACK_TIMEOUT + PACK_TIMEOUT * Random::uniform(1.0));
}
void Monitor::handleTap(const Packet* packet)
{
Behavior behavior;
hdr_sr *srh = hdr_sr::access(packet);
hdr_ip *iph = hdr_ip::access(packet);
hdr_cmn *cmh = hdr_cmn::access(packet);
ID cur_hop(srh->addrs()[srh->cur_addr()-1]);
nsaddr_t cur_addr = cur_hop.getNSAddr_t();
int uid = cmh->uid();
map<int, PackData*>::iterator it;
it = pack_t.find(uid);
I am getting the following errors:
**monitor.h: error: ‘PackTableTimer’ does not name a type
monitor.cc: In constructor ‘Monitor::Monitor(DSRAgent*)’
monitor.cc: error: ‘class Monitor’ has no member named ‘PackTableTimer’
monitor.cc: error: ‘pack_table_timer’ was not declared in this scope**
You need to forward-declare PackTableTimer before you can use it in monitor.h
Put this with your other forward-declarations.
class PackTableTimer;
You miss declaration of PackTableTimer class.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
As the title says when I run my program, visual studio, give me that error. The class that produce that error is the following:
class UsesScene {
BaseScene & scene;
public:
UsesScene(BaseScene& scene) : scene(scene) {}
void Start() { scene.Start(); }
void EventHandler(SDL_Event ev) { scene.EventHandler(ev); }
void Update() { scene.Update(); }
void Draw(SDL_Renderer* renderer) { scene.Draw(renderer); }
};
The popup appears after } at the end of the initialization of the Start void.
Another code that can interact with these classes is the following:
void GameManager::regScene(UsesScene scene) {
if (display != NULL) {
UsesScene* ptr = &scene;
UsesScene** pptr = &ptr;
display = *pptr;
}
else
{
UsesScene* ptr = &scene;
UsesScene** pptr = &ptr;
buffer = *pptr;
}
}
the display & buffer are declared as
class GameManager
{
private:
SDL_Window* window;
SDL_Renderer* renderer;
UsesScene* display;
UsesScene* buffer;
bool isRunning;
bool reStart;
};
and for access to the display or buffer's parameter I use the following syntax:
display->Start();
and the last thing that has to do with this class is this part of code:
TestScene tScene;
UsesScene testScene(tScene);
this->regScene(testScene);
Test Scene simply extends BaseScene:
class BaseScene
{
private:
bool sceneloop = false;
public:
virtual void Start() { std::cout << "BasceScene::Start()" << std::endl; };
virtual void EventHandler(SDL_Event event) {};
virtual void Update() {};
virtual void Draw(SDL_Renderer* renderer) {};
void _toggleLoopMode() { sceneloop = !sceneloop; }
bool _sceneloop() { return sceneloop; }
};
How I can solve this?
ps:
some code that doesn't interfere with the Display and Buffer wasn't reported, if you need: free to ask
So, after a few hours of coding, thanks to Remy Lebeau, I've finally resolved this issue.
First I've changed the regScene() method and now looks like this:
void GameManager::regScene(UsersScene *scene) {
if (SceneManager::_display == NULL)
_display = scene;
else
_buffer = scene;
}
And then I pass the scene like this:
TestScene tScene;
UsersScene testScene(tScene);
sManager->regScene(&testScene);
Thanks to all.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Given a header file that has different functions (function1 and function2): what is the best practice of sharing the same default arguments?
class MyClass {
public:
virtual ... function1(..., int param1 = 48, int param2 = getStuff(99)) = 0;
virtual ... function2(..., int param1 = 48, int param2 = getStuff(99)) = 0;
}
Using something like
#define PARAM1 = 48
#define PARAM2 = getStuff(99)
seems rather inconvenient. I am fairly new to C++ so I don't know whats the best practice in this case. Should I instead create some private constant inside of MyClass?
You may do:
class MyClass {
public:
void function1(int param1 = default_param1, int param2 = default_param2());
void function2(int param1 = default_param1, int param2 = default_param2());
private:
static const int default_param1 = 48;
static int default_param2() { return getStuff(99); } // assuming it might change
};
I see your functions are virtual - be aware that the default parameters are not inherited!
int getStuff(int n)
{
return n;
}
class MyClass
{
public:
static constexpr int DefaultParam1 = 48;
static constexpr int DefaultParam2 = 99;
virtual ~MyClass() = default;
virtual void function1(int param1 = DefaultParam1, int param2 = DefaultParam2) = 0;
inline void function2()
{
function1(getStuff(DefaultParam1), getStuff(DefaultParam2));
}
inline void function2(int param1)
{
function1(param1, getStuff(DefaultParam2));
}
virtual void function2(int param1, int param2) = 0;
};
Have a look at function2 in my example, it is overloaded with inline functions. Advantage:
class MC : public MyClass
{
public:
virtual void function1(int param1, int param2)
{
}
using MyClass::function2;
virtual void function2(int param1, int param2)
{
}
};
int main()
{
MC mc;
((MyClass&)mc).function1();
mc.function2();
return 0;
}
function1 requires a cast to be used with default parameters (without repeating them in the inheriting class), function2 does not (unfortunately, you need the using clause to make the overloads visible in the inheriting class, though).
Side note: I'm not telling the first approach is wrong or bad, I'm just showing an alternative...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm building an engine. I need to create a Timer class which will call a function by a pointer from a separate class. For example:
class MyTimer {
public:
void setTimeoutFunction( _pointer_, unsigned short timeoutMs ) {
// here we need to have a opportunity to store a _pointer_ to a function
}
void tickTimer() {
...
// here I need to call a function by a pointer
...
}
};
// Main class:
class MyAnyClass {
public:
void start() {
MyTimer myTimer;
myTimer.setTimeoutFunction( startThisFunc, 1500 ); // 1500ms = 1.5s
while ( true ) {
myTimer.tickTimer();
}
}
void startThisFunc() { ... }
}
In summation, how do you store a pointer to a function which belongs to some class and call that function by a pointer?
For your requirements, I might recommend making the timer a class template:
template <typename T>
struct MyTimer
{
using FuncPtr = void (T::*)();
MyTimer(FuncPtr ptr, T * obj, unsigned int timeout_ms)
: ptr_(ptr), obj_(obj), timeout_ms_(timeout_ms) {}
void tickTimer()
{
(obj_->*ptr_)();
}
FuncPtr ptr_;
T * obj_;
unsigned int timeout_ms_;
};
Usage:
struct MyAnyClass
{
void start()
{
MyTimer<MyAnyClass> myTimer(&MyAnyClass::startThisFunc, this, 1500);
while (true) { myTimer.tickTimer(); }
}
void startThisFunc() { /* ... */ }
};
In C++11 you can use std::function. A good guide on using it is here: http://en.cppreference.com/w/cpp/utility/functional/function
I created a new code snippet only containing the case you want.
#include <stdio.h>
#include <functional>
#include <iostream>
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
int main()
{
// store a call to a member function
std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
const Foo foo(314159);
f_add_display(foo, 1);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store class member function address, to local data structure(table)
typedef struct
{
unsigned int id;
void (TCLASS::*proc)();
} TSTRUCT;
class TCLASS{
public:
void tfunct();
const TSTRUCT t1 = { 1, &tfunct};
};
Though you didn't write a question, assuming you are facing bunch of compiler errors.
See below :
class TCLASS ; //forward declaration
struct TSTRUCT
{
unsigned int id;
void (TCLASS::*proc)( );
// // Use the TSTRUCT constructor
TSTRUCT(int i, void (TCLASS::*fptr)( ) ): id(i), proc(fptr)
{
}
} ;
class TCLASS{
public:
void tfunct();
TCLASS() : t1(1, &TCLASS::tfunct ) // initialize the const member
//~~~~~~~~~~~^ Use &TCLASS::tfunct instead of &tfunct
{
}
const TSTRUCT t1;
};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
As a practice program after learning C++, I am developing a text-based game. I am using object-oriented programming style for handling the worlds/their objects. Here's the necessary information about their definitions:
class Object
{
private:
unsigned int id;
public:
unsigned int getID() const { return id; }
};
class TakeableObject: public Object
{
...
};
class EdibleObject: public TakeableObject
{
private:
float healthEffect;
float staminaEffect;
public:
float getHealthEffect() const { return healthEffect; }
float getStaminaEffect() const { return staminaEffect; }
};
class Player
{
private:
float health;
float stamina;
TakeableObject inventory[256];
public:
eat(const EdibleObject* o)
{
health += o->getHealthEffect();
stamina += o->getStaminaEffect();
}
eat(int id)
{
if (inventory[id] == NULL)
throw "No item with that ID!";
eat((EdibleObject) inventory[id]);
inventory[id] = NULL;
}
};
So my question is - in Player::eat(int), is there a way I can make sure the Object at Player::inventory[id] is an EdibleObject (perhaps through exception handling?)
User dynamic cast to check the object type at runtime.
Or you can use a virtual function with default definition in parent and can update it as per your requirement in derived classes.
Instead of eat((EdibleObject) inventory[id]); use the following
EdibleObject *temp = dynamic_cast<EdibleObject *>( &inventory[id] );
if(temp) { eat(*temp); }
else { /* Handling */ }
Your code suffers Object splicing, make sure to get rid of that first.