-2

I have the following code :

class FLOAT    
{
    float *num;

public:
    FLOAT(){}
    FLOAT(float f)
    {
        num = new float(f);
    }

    FLOAT operator +(FLOAT& obj)
    {
        FLOAT temp;
        temp.num = new float;

        temp.num = *num + obj.getF();
        return temp;    
    }

    float getF(){ return *num; }
    void showF(){ cout << "num : "<< *num << endl; }
};

It is showing an error.

My question is, how do I access that float *num data member using class object?

  • 1
    Time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Aug 03 '18 at 05:50
  • 2
    Also please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ to learn some reasons your question might be down-voted. Finally, please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Aug 03 '18 at 05:50
  • And it seems to me that either you have misunderstood something basic about C++ and variables/object creation, or you come from a Java or C# background. C++ is *not* Java or C#, you don't need `new` to create objects. [Here's a list of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), if you want to learn then I suggest you get a few of them and start all over. – Some programmer dude Aug 03 '18 at 05:51
  • Sidenote: [The Principle of RAII](https://en.cppreference.com/w/cpp/language/raii) suggests a destructor to clean up the allocation `num` points at. This leads to [The Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three). The Rule of Zero is what you *really* want to aim for in C++, and that suggests `num` should be a plain old `int` if possible. – user4581301 Aug 03 '18 at 06:01
  • To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Aug 03 '18 at 10:45

2 Answers2

5

There are a lot of mistakes in your class. It is simply not setup correctly.

  • The class's default constructor is not allocating the float at all.

  • the class is not following the Rule of 3/5/0. It is missing a destructor to free the float, a copy constructor and copy assignment operator to make safe copies of the float, and in C++11 and later, it is missing a move constructor and move assignment operator to safely move the float between objects.

  • your operator+ is not dereferencing the pointer when assigning a new value to the float.

Try this instead:

class FLOAT
{
    float *num;

public:
    FLOAT(float f = 0) : num(new float(f)) {}

    FLOAT(const FLOAT &src) : num(new float(*(src.num))) {}

    // in C++11 and later...
    FLOAT(FLOAT &&src) : num(src.num) { src.num = nullptr; }
    // alternatively:
    // FLOAT(FLOAT &&src) : num(nullptr) { std::swap(num, src.num); }

    ~FLOAT() { delete num; }

    FLOAT& operator=(const FLOAT &rhs)
    {
        *num = *(rhs.num);
        return *this;
    }

    // in C++11 and later...
    FLOAT& operator=(FLOAT &&rhs)
    {
        std::swap(num, rhs.num);
        return *this;
    }

    FLOAT operator+(const FLOAT& rhs)
    {
        FLOAT temp;
        *(temp.num) = *num + rhs.getF();
        return temp;

        // or simply:
        // return *num + rhs.getF();
    }

    float getF() const { return *num; }
    void showF() { cout << "num : " << *num << endl;    }
};

That being said, there is no good reason to dynamically allocate the float at all (except maybe as a learning experience). Let the compiler handle the memory management for you:

class FLOAT
{
    float num;

public:
    FLOAT(float f = 0) : num(f) {}

    FLOAT(const FLOAT &src) : num(src.num) {}

    FLOAT& operator=(const FLOAT &rhs)
    {
        num = rhs.num;
        return *this;
    }

    FLOAT operator+(const FLOAT& rhs)
    {
        FLOAT temp;
        temp.num = num + rhs.getF();
        return temp;

        // or simply:
        // return num + rhs.getF();
    }

    float getF() const { return num; }
    void showF() { cout << "num : " << num << endl; }
};

Which can then be simplified a little by letting the compiler implicitly define the copy constructor and copy assignment operator for you:

class FLOAT
{
    float num;

public:
    FLOAT(float f = 0) : num(f) {}

    FLOAT operator+(const FLOAT& rhs)
    {
        FLOAT temp;
        temp.num = num + rhs.getF();
        return temp;

        // or simply:
        // return num + rhs.getF();
    }

    float getF() const { return num; }
    void showF() { cout << "num : " << num << endl; }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

When you assign bellow statement :

temp.num = *num + obj.getF();

Actually you assign a float number to a float pointer!

So, use of bellow :

(*temp.num) = (*num) + obj.getF();

Instead of :

temp.num = *num + obj.getF();