[galib] Problems with Vectors and UserData

Jose daVeiga jfernand at ucla.edu
Sat Mar 19 15:09:39 EST 2005


I to all.  I am (still) having problems with UserData.
Now it's my inability to get it to work with the Vector class.

The way I am including vector is as follows:
	#include <vector>
	using std::vector; 
This was the only way I managed to get vector to work with the Galib. 
>From what I could gather it's because GAlib uses templates(?)...

Following one of the examples, here is how I define UserData:
//Data Structure for Passing MAYA Stuff to genome/objective
	typedef struct _UserData {
 		MDagPath objectPath;
 		MObject component;
 		vector<float> *genomeValues;  //<---is this correct?
	} UserData;

The vector genomeValues is to contain the initial value for each gene;
it should be passed on within UserData and used to set each gene when
DeterministicInitializer is called.

When I init things,I try to set the vector values within the UserData
structure this way:
	vector<float> initGenomeValues(3);
	//get the initial values from MAYA into initGenomeValues
	getCVsInitialPosition(initGenomeValues, 
				    initObjectPath ,
				    initComponent);
	data.objectPath = initObjectPath;
 	data.component = initComponent;
 	data.genomeValues = &initGenomeValues;

Up until here all this compiles fine
But then my DeterministicInitializer does not compile when I do the
following to get the values from UserData
	void DeterministicInitializer(GAGenome& g)//, vector<float> v)
	{
		GARealGenome& genome =(GARealGenome&)g;
		UserData * data = (UserData *)g.userData();
		int geneLength = genome.length()-1;
		int counter = 0;
		for(int i = 0; i < geneLength; i++) 
		{
			//The problem seems to be here!\/
			genome.gene(i, data.genomeValues[counter+2]);
			counter = counter + 3;
}

The error I get when compiling is:
	"... error C2228: left of '.genomeValues' must have
class/struct/union type"
I am using MS-VC++ 6.0

Anyway, you get the point.  This is probably not due to any specific
Galib issue but rather because I am a novice to C++. In fact it is
likelly due to my lack of experience with pointers, references, data
structures, etc...and with using vectors. However, and as usual, any
help is welcome since I have done a whole lot of reading on this and
tried a bunch of different scenarios and I am still stuck!  

Best to all, 

Jose

PS- FYI - when I take out the vector from the UserData structure and I
use it as a global which I access within DeterministicInitializer, all
this works.  I am trying to use UserData to pass the GA all of the info
and not use a global scope variable ... for consistency and proper
coding standards...



More information about the galib mailing list