Sign in to follow this  

static member definining/declaration problems

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Update: Okay I have tried to update my files but am having trouble declaring/defining the vector. I've added this to "event.h"
static vector<baseEvent*> anotherList;



and this to "event.cpp"
vector<baseEvent*> baseEvent::anotherList;



but it creates linking errors. Original: Okay, I've been working on another event Handler project. The following Code compiles in visual express. I can step through the program and it adds and flagEvent to the static "eventList". But once I return to the main method to "run()", "eventList" shows itself as empty. Can anyone tell me what's going on?
#include <iostream>
#include "object.h"
#include "event.h"
using namespace std;

int main(int argc, char** argv) {
	object asdf;
	asdf.createEvent();
	baseEvent::run();
}





#ifndef OBJECT_H
#define OBJECT_H

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

#include "event.h"

class object {
public:
	billy bob;
	int doSomething ();
	void createEvent();
};

#endif





#include "object.h"

int object::doSomething () {
	if (bob == true) { cout << "it worked"; }
	return 0;
};
void object::createEvent() {
	flagEvent <object> blah;
	bob = true;
	blah.add(this, &object::doSomething);
	//eventList[0]->execute();
};





#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 billy {
public:
	bool flag;
	billy() {
		flag = false;
	}
	void operator=(bool op2) {
		flag = op2;
	}
	bool operator==(bool op2) {
		return flag == op2;
	}
};

template <class T>
class flagEvent : baseEvent{
public:
	int (T::*pt2Member)();
	T* objectPtr;
	void add(T* objectPtr, int (T::*_fptr) ()) {
		flagEvent* newEvent = new flagEvent();
		(*newEvent).objectPtr = objectPtr;
		(*newEvent).pt2Member = _fptr;
		eventList.push_back(newEvent);

	}
	int execute() {
		return (*objectPtr.*pt2Member) ();

	}
};

#endif





#include "event.h"

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





[Edited by - TruckMack33 on December 7, 2008 7:28:25 PM]

Share this post


Link to post
Share on other sites
Static objects by default in C++ have internal linkage. That means that every file which includes event.h will get its own static copy of the eventList. So if you manipulate eventList from one .cpp file, it won't change the eventList in a different .cpp file.

The solution is to declare eventList as extern rather than static. Then define it once in one of your .cpp files.

Share this post


Link to post
Share on other sites
Okay, i tried doing that.
I kept eventList in event.h as:

vector<baseEvent*> eventList;

and i put the following into object.h and main.cpp:

extern vector<baseEvent*> eventList;


I get the following error:

1>main.obj : error LNK2005: "class std::vector<class baseEvent *,class std::allocator<class baseEvent *> > eventList" (?eventList@@3V?$vector@PAVbaseEvent@@V?$allocator@

I tried keeping the static keyword but that produces another error. Any suggestions?

Share this post


Link to post
Share on other sites
Put

vector<baseEvent*> eventList;

in event.cpp instead, end

extern vector<baseEvent*> eventList;

in event.h. This makes eventList visible to all files that include event.h, while the extern tells the compiler that the variable is declared elsewhere, so that it doesn't allocate memory for eventList every time someone includes event.h. However, eventList should only be accessed by baseEvent and it's derived classes, so why is eventList not a static member of baseEvent?

Share this post


Link to post
Share on other sites
What you suggest compiles and works. Thanks.

Your right. It should be a private static variable of baseClass but I've been fooling around and can't figure out how to do it. It either gives me a cannot access error or an unresolved linking error.

BTW, I also had the idea to make execute() and two variables in flagEvent private.
I've made that work successfully.

Share this post


Link to post
Share on other sites

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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