JA Guerrero JA Guerrero - 18 days ago 6
C++ Question

How to use the values of a variable belonging to a class in another class?

I have this code everything is separated by .h file and. cpp: Fitness.h

class Fitness

// Professor's ID
float _fit;


// Inicia los datos del profesor
Fitness(float fit);

// Devuelve professor's ID
inline float GetFit() const { return _fit; }



#include "stdafx.h"
#include "Fitness.h"

// Inicializa los datos del fit este es el archivo cpp
Fitness::Fitness(float fit) : _fit(fit) { }

and the part where I use it is here in a class called file called configuracion: Configuracion.h

class Configuracion

//Lectura del Fitness
hash_map<float, Fitness*> _fit;

// Indica que aun no se lee la informaciĆ³n
bool _isEmpty;


// Inicia datos
Configuracion() : _isEmpty(true) { }

// Libera recursos usados

// Analiza datos y guarda datos
void ParseFile(char* fileName);

inline Fitness* GetFit(float fit)
hash_map<float, Fitness*>::iterator it = _fit.find(fit);

return it != _fit.end() ? (*it).second : NULL;

inline float GetNFit() const
return (float)_fit.size();

Fitness* ParseFitness(ifstream& file);

// Remueve caracteres vacios al principio y fin de la cadena
string& TrimString(string& str);


for (hash_map<float, Fitness*>::iterator it = _fit.begin(); it != _fit.end(); it++)
delete (*it).second;

void Configuracion::ParseFile(char* fileName)
// Limpia objetos previamente analizados

string line;
while( input.is_open() && !input.eof() )
// Leer linea por linea hasta que se obtenga el inicio de un nuevo objeto
getline( input, line );
TrimString( line );

// Lee, analiza y guarda el tipo de objeto
if (line.compare("#fit") == 0)
Fitness* f = ParseFitness(input);

if (f)
_fit.insert(pair<float, Fitness*>(f->GetFit(), f));

}//////////////////cambiar variable o modificar

_isEmpty = false;

To the use the value of the variable in another file within another class:

float f1 = 0.000000;
//f1 = Configuracion::GetInstance().GetFit();

f1 =(&Configuracion::_fit);

//Horario * f1= &Configuracion::_fit;
if (best->GetFitness() >= 0.45000000)
// ...

In this line I have an error :

f1 =(&Configuracion::_fit);

This is the error:

Error 2 error C2440: '=' : cannot convert from 'stdext::hash_map<float,Fitness *,stdext::hash_compare<_Kty,std::less<_Kty>>,std::allocator<std::pair<const _Kty,_Ty>>> Configuracion::* ' to 'float'


Well there are two major problems with this line:

f1 =(&Configuracion::_fit);

First of all, _fit is a non-static member of the Configuracion class, which means that you can't access it unless you refer to an instance of Configuracion. For example:

Configuracion myConfiguracion;  // Create instance of Configuracion.

f1  = myConfiguracion._fit;     // Access member variable.

Second, Configuracion::_fit has the type hash_map<float, Fitness*>. You cannot assign a hash_map object to a float because their types are incompatible. It's difficult to see what you're trying to achieve, so I can't tell you how to fix it. You should re-evaluate the function in question and fix accordingly.