problem with string&

S

sernamar

Hi,
I have the following base class

class UnitOfMeasure
{
public:
//...
std::string& GetName() {return _uomName;};
//...
protected:
std::string _uomName;
//...
};

And the subclass

class UomAngle : public UnitOfMeasure
{
public:
UomAngle();

UomAngle(const UnitOfMeasure &uom);

virtual ~UomAngle();

};

When I create an UomAngle object (called radian) and use it, all works
fine

cout<<radian->GetName();

Then, I have another base class

class Measure
{
public:
Measure();
virtual ~Measure();

double GetValue(){return _value;};
std::string& GetUomName(){return _uom->GetName();};

protected:
double _value;
private:
UnitOfMeasure *_uom;
};

And the subclass

class Angle : public Measure
{
public:
Angle(double value,const std::string& uomName);

private:
UomAngle *_uom;
};

And I have the following problems:

1.- When I create an Angle and I want to get the uom name

Angle a1(180,degree);
cout<<a1.GetValue()<<"\t";
cout<<a1.GetUomName();

it doesn't work. If I put the method

std::string& GetUomName(){return _uom->GetName();};

in the class Angle it works. But, why I have to do this? How can I do
it work without putting GetUomName in each subclass of Measure?

2.- The other problem is that

Angle a1(180,degree);
cout<<a1.GetValue()<<"\t";
cout<<a1.GetUomName();

doesn't work because I have a problem with returning string&. I think
that I have a problem because in

std::string& GetUomName(){return _uom->GetName();};

_uom->GetName() works like a local variable, and I'm returning a
reference to this local variable.

Any idea to solve those problems or more problems that I could have?
Thanks!
 
A

amparikh

sernamar said:
Hi,
I have the following base class

class UnitOfMeasure
{
public:
//...
std::string& GetName() {return _uomName;};
//...
protected:
std::string _uomName;
//...
};

And the subclass

class UomAngle : public UnitOfMeasure
{
public:
UomAngle();

UomAngle(const UnitOfMeasure &uom);

virtual ~UomAngle();

};

When I create an UomAngle object (called radian) and use it, all works
fine

cout<<radian->GetName();

Then, I have another base class

class Measure
{
public:
Measure();
virtual ~Measure();

double GetValue(){return _value;};
std::string& GetUomName(){return _uom->GetName();};

protected:
double _value;
private:
UnitOfMeasure *_uom;
};

And the subclass

class Angle : public Measure
{
public:
Angle(double value,const std::string& uomName);

private:
UomAngle *_uom;
};

And I have the following problems:

1.- When I create an Angle and I want to get the uom name

Angle a1(180,degree);
cout<<a1.GetValue()<<"\t";
cout<<a1.GetUomName();

it doesn't work. If I put the method

std::string& GetUomName(){return _uom->GetName();};

in the class Angle it works. But, why I have to do this? How can I do
it work without putting GetUomName in each subclass of Measure?

Probably because you are not initializing the resp instance member
within Angle and Measure correctly ?
Both seemed to have a pointer named _uom and maybe that is messing up
stuff in your code(you have not posted all the code, so this is just a
guess).
 
A

Andre Kostur

Hi,
I have the following base class

class UnitOfMeasure
{
public:
//...
std::string& GetName() {return _uomName;};
//...
protected:
std::string _uomName;
//...
};

And the subclass

class UomAngle : public UnitOfMeasure
{
public:
UomAngle();

UomAngle(const UnitOfMeasure &uom);

virtual ~UomAngle();

};

When I create an UomAngle object (called radian) and use it, all works
fine

cout<<radian->GetName();

Then, I have another base class

class Measure
{
public:
Measure();
virtual ~Measure();

double GetValue(){return _value;};
std::string& GetUomName(){return _uom->GetName();};

protected:
double _value;
private:
UnitOfMeasure *_uom;
};

And the subclass

class Angle : public Measure
{
public:
Angle(double value,const std::string& uomName);

private:
UomAngle *_uom;
};

And I have the following problems:

1.- When I create an Angle and I want to get the uom name

Angle a1(180,degree);
cout<<a1.GetValue()<<"\t";
cout<<a1.GetUomName();

it doesn't work. If I put the method

std::string& GetUomName(){return _uom->GetName();};

in the class Angle it works. But, why I have to do this? How can I do
it work without putting GetUomName in each subclass of Measure?

2.- The other problem is that

Angle a1(180,degree);
cout<<a1.GetValue()<<"\t";
cout<<a1.GetUomName();

doesn't work because I have a problem with returning string&. I think
that I have a problem because in

std::string& GetUomName(){return _uom->GetName();};

_uom->GetName() works like a local variable, and I'm returning a
reference to this local variable.

Any idea to solve those problems or more problems that I could have?
Thanks!

I don't see anywhere in there where _uom is ever pointing to anything
useful.
 
S

sernamar

Here is the full code:

// UnitOfMeasure.h
//
#include <string>

class UnitOfMeasure
{
public:
UnitOfMeasure(){};

UnitOfMeasure(std::string &uomID,
std::string &uomName,
std::string &uomSymbol,
int measureType,
std::string &nameStandardUnit,
double scaleToStandardUnit,
double offsetToStandardUnit,
std::string &formula);

virtual ~UnitOfMeasure();

std::string& GetID() {return _uomID;};
std::string& GetName() {return _uomName;};
std::string& GetStandardName() {return _nameStandardUnit;};

std::string& GetSymbol() {return _uomSymbol;};

double ConvertToStandard(double value);
double ConvertFromStandard(double value);

protected:
std::string _uomID;
std::string _uomName;
std::string _uomSymbol;
int _measureType;
std::string _nameStandardUnit;
double _scaleToStandardUnit;
double _offsetToStandardUnit;
std::string _formula;
};

// UnitOfMeasure.cpp
//
#include "UnitOfMeasure.h"

using namespace std;

UnitOfMeasure::UnitOfMeasure (std::string &uomID,
std::string &uomName,
std::string &uomSymbol,
int measureType,
std::string &nameStandardUnit,
double scaleToStandardUnit,
double offsetToStandardUnit,
std::string &formula):
_uomID(uomID),
_uomName(uomName),
_uomSymbol(uomSymbol),
_measureType(measureType),
_nameStandardUnit(nameStandardUnit),
_scaleToStandardUnit(scaleToStandardUnit),
_offsetToStandardUnit(offsetToStandardUnit),
_formula(formula)
{
}

UnitOfMeasure::~UnitOfMeasure()
{
}

double UnitOfMeasure::ConvertToStandard(double value)
{
return value * _scaleToStandardUnit + _offsetToStandardUnit;
}

double UnitOfMeasure::ConvertFromStandard(double value)
{
return (value - _offsetToStandardUnit) / _scaleToStandardUnit;
}

// UomAngle.h
//
class UomAngle : public UnitOfMeasure
{
public:
UomAngle();

UomAngle(const UnitOfMeasure &uom);

virtual ~UomAngle();

};

// UomAngle.cpp
//
#include "UomAngle.h"

UomAngle::UomAngle():UnitOfMeasure()
{
}

UomAngle::UomAngle(const UnitOfMeasure &uom):UnitOfMeasure(uom)
{
}

UomAngle::~UomAngle()
{
}

// Measure.h
//
#include <string>
#include "UnitOfMeasure.h"

class Measure
{
public:
Measure();

virtual ~Measure();

double GetValue(){return _value;};
std::string& GetUomName(){return _uom->GetName();};
std::string& GetUomSymbol() {return _uom->GetSymbol();};

protected:
double _value;
private:
UnitOfMeasure *_uom;
};

// Measure.cpp
//
#include "Measure.h"

Measure::Measure()
{
}

Measure::~Measure()
{
}

// Angle.h
//
#include "Measure.h"
#include "UomAngle.h"

class Angle : public Measure
{
public:
Angle();

Angle(double value,const std::string& uomName);

virtual ~Angle();

std::string& GetUomName(){return _uom->GetName();};
std::string& GetUomSymbol() {return _uom->GetSymbol();};

Angle ConvertToStandard();
Angle ConvertFromStandard(const std::string& uomName);
Angle ConvertTo(const std::string& uomName);

private:
UomAngle *_uom;
Angle(double value,UomAngle* uom);
};

// Angle.cpp
//
#include "Angle.h"
#include "UomManager.h"

using namespace std;

Angle::Angle()
{
}

Angle::Angle(double value,const string &uomName)
{
_value = value;
UomManager *uomMng = UomManager::GetInstance();
_uom = uomMng->GetUomAngle(uomName);
}

Angle::Angle(double value,UomAngle* uom)
{
_value = value;
_uom = uom;
}

Angle::~Angle()
{
}

Angle Angle::ConvertToStandard()
{
string stdname(_uom->GetStandardName());
if (_uom->GetName() == stdname) return *this;
else {
UomManager *uomMng = UomManager::GetInstance();
UomAngle *stduom = uomMng ->GetUomAngle(stdname);
double newvalue = _uom->ConvertToStandard(_value);
return Angle(newvalue,stduom);
}
}

Angle Angle::ConvertFromStandard(const std::string& uomName)
{
if (_uom->GetName() == uomName) return *this;
else {
UomManager *uomMng = UomManager::GetInstance();
UomAngle *uom = uomMng ->GetUomAngle(uomName);
double newvalue = uom->ConvertFromStandard(_value);
return Angle(newvalue,uom);
}
}

Angle Angle::ConvertTo(const std::string& uomName)
{
if (_uom->GetName() == uomName) return *this;
else {
UomManager *uomMng = UomManager::GetInstance();
UomAngle *uom = uomMng ->GetUomAngle(uomName);
double stdvalue = _uom->ConvertToStandard(_value);
double newvalue = uom->ConvertFromStandard(stdvalue);
return Angle(newvalue,uom);
}
}


note: UomManager is a unit of measure manager that allows to get a
pointer to a UomAngle with the method GetUomAngle(uomName), where
uomName is a string.

//main.cpp
//
#include <cstdlib>
#include <iostream>
#include <iomanip>

#include <uommanager.h>
#include <angle.h>

using namespace std;

int main(int argc, char *argv[])
{
string degree("degree");

Angle a1(180,degree);
cout<<a1.GetValue()<<"\t";
cout<<a1.GetUomSymbol();
cout<<" ("<<a1.GetUomName()<<")"<<endl<<endl;

system("pause");
return EXIT_SUCCESS;
}

That's all. If you need something more, just tell me.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top