Sign in to follow this  
TruckMack33

Event Handling Using GlutTimerFunc

Recommended Posts

Okay, I created an event class and pushed instances on a vector. I plan on using GlutTimerFunc to review the vector after a given time delay. This way, I can multitask and the program can stay responsive. I have created the following code, but can't figure out this last error and how to fix it. The error is error C2664: 'spec::spec' : cannot convert parameter 1 from 'bool *' to 'const spec &' 1> Reason: cannot convert from 'bool *' to 'const spec' 1> No constructor could take the source type, or constructor overload resolution was ambiguous
#ifndef EVENT_H
#define EVENT_H

#include <vector>
#include <iostream>
using namespace std;

class baseEvent {
public:
	virtual int execute() = 0;
	static void run();
};

static vector<baseEvent*> eventList;

class flagEvent: public baseEvent {
protected:
	bool* eventFlag;
public:
	flagEvent(bool* _eventFlag);
	void add();
	int execute();
};

class spec: public flagEvent {
public:
	int execute();
};

#endif

//event.cpp
#include "event.h"

void baseEvent::run(){
	for(int i = 0; i < eventList.size();) {
		if(eventList[i]->execute() == 0) {
			delete eventList[i];
			eventList.erase(eventList.begin()+i,eventList.begin()+i+1);
		} else {
			i++;
		}
	}
}



flagEvent::flagEvent(bool* _eventFlag) { eventFlag = _eventFlag; }
void flagEvent::add() {
	if(*eventFlag == false) {
		*eventFlag = true;
		flagEvent* newEvent = new flagEvent(this->eventFlag);
		eventList.push_back(newEvent);
	}
}
int flagEvent::execute() { 
	cout << "flagEvent"; 
	*eventFlag = false;
	return 0;
}

int spec::execute() { 
	cout << "spec"; 
	*eventFlag = false;
	return 0;
}

#include <iostream>
#include <map>
#include <vector>
using namespace std;

#include "event.h"

//main.cpp
class item {
private:
	bool eventFlag;
public:
	item () { eventFlag = false; }
	void createEvent() {
		spec newEvent(&eventFlag);  <-- error occurs here
		newEvent.add();
	}
};

int main(int argc, char** argv) {
	item foo;
	foo.createEvent();
	foo.createEvent();
	baseEvent::run();

	foo.createEvent();
	baseEvent::run();
	int num;
	cin >> num;
}

[Edited by - macktruck6666 on July 29, 2008 12:52:32 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by macktruck6666
Okay, I created an event class and pushed instances on a vector. I plan on using GlutTimerFunc to review the vector after a given time delay. This way, I can multitask and the program can stay responsive. I have created the following code, but can't figure out this last error and how to fix it.

The error is
error C2664: 'spec::spec' : cannot convert parameter 1 from 'bool *' to 'const spec &'
1> Reason: cannot convert from 'bool *' to 'const spec'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

(1) You can use the [ source ] tags. It makes your code more readable.
(2) You do not provide a constructor for your spec class that takes a boolean argument. In C++ constructors are not inherited. Perhaps you mean this.

class spec: public flagEvent
{
public:
spec(bool* eventFlag)
: flagEvent(eventFlag)
{ }

int execute();
};

Share this post


Link to post
Share on other sites
Okay, i copied and pasted the code and added an extra semicolon in the body. It runs but prints out "flagevent" instead of "sec". It's executing the execute for flagevent instead of sec.

I've tried adding "virtual" keyword before the flagevent execute method. It does the same thing.

I've tried copying the constructor to the sec class and changeling everything from flagevent to sec. But then I get a whole bunch of errors.

I want it to do exactly the same thing flageven constructor does, but I want it to run it's own execute method.

[Edited by - macktruck6666 on July 29, 2008 1:55:05 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by macktruck6666
Okay, i copied and pasted the code and added an extra semicolon in the body. It runs but prints out "flagevent" instead of "sec". It's executing the execute for flagevent instead of sec.

Now the problem appears to be the add() member function. It's creating a new flagEvent object and adding to the eventList, even if executed on a spec object.

Try creating a spec object and pushing it onto the eventList explicitly and see what happens.

Share this post


Link to post
Share on other sites
I assume that your correct. I created a add method for the spec class that adds a spec instance to the vector and it works like it should. I'm now wondering if there is someway to not have to create an add() method for each child of flagEvent. Hopefully something similar to what was done for the constructor.

Does anyone know if creating a template might help?

[Edited by - macktruck6666 on July 29, 2008 3:43:25 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this