problem with string&

Discussion in 'C++' started by sernamar, Nov 15, 2006.

  1. sernamar

    sernamar Guest

    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!
    sernamar, Nov 15, 2006
    #1
    1. Advertising

  2. sernamar

    Guest

    sernamar wrote:
    > 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).

    >
    > 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!
    , Nov 15, 2006
    #2
    1. Advertising

  3. sernamar

    Andre Kostur Guest

    "sernamar" <> wrote in news:1163628427.605768.296020
    @b28g2000cwb.googlegroups.com:

    > 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.
    Andre Kostur, Nov 15, 2006
    #3
  4. sernamar

    sernamar Guest

    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
    sernamar, Nov 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    718
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,548
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    471
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,854
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,782
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page