0

No error is shown while running, but the output comes like this

f1=6.72623e-044
f2=0
f3=6.72623e-044

When I intialize by a=5, A warning comes like non-static data member initializers only available with -std c plus plus. And the output becomes f1=5 f2=5 f3=10

#include<iostream>
#include<cstdio>
using namespace std;
class FLOAT
{
    float a;
    public:
        FLOAT(){}
        FLOAT(float x)
        {
            x=a;
        }
        FLOAT operator +(FLOAT f);
        void display(void);
};
FLOAT FLOAT::operator +(FLOAT f)
{
    FLOAT t;
    t.a=a+f.a;
    return t;
}
void FLOAT::display(void)
{
    cout<<a<<endl;
}
int main()
{
    FLOAT f1,f2,f3;
    f1=FLOAT(3.6);
    f2=FLOAT(5.8);
    f3=f1+f2;
    cout<<"f1="; f1.display();
    cout<<"f2="; f2.display();
    cout<<"f3="; f3.display();
    return 0;
}
Rev99
  • 35
  • 7
  • Please copy and paste errors and warnings into the question instead of trying to paraphrase it. Details in error messages can matter. – eesiraed May 11 '20 at 17:54
  • Please see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. Step through your code with a debugger (pay attention to the constructor) and you'll see your bug. – eesiraed May 11 '20 at 17:56
  • 1
    in your constructor: `x=a;` please read that again. Voting to close as typo – 463035818_is_not_an_ai May 11 '20 at 18:03
  • Check the compiler warnings. I get two warnings and both provide very strong hints as to what happened: *warning: parameter 'x' set but not used* and *'a' is used uninitialized in this function*. Never ignore warnings. They are the compiler telling you that while the code is syntactically correct and can be transformed into an executable, that executable probably doesn't do what you expect. – user4581301 May 11 '20 at 18:04

1 Answers1

1

This constructor doesn't make much sense, you are assigning the value of uninitialized a to the parameter x, which in term is never used:

FLOAT(float x)
{
    x = a; //<-- here
}

I guess what you want is:

FLOAT(float x)
{
    a = x; //<-- here
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Thanks man. I got it. It's a silly mistake of me to assign a parameter to the uninitialized a. Thanks for the response. – Rev99 May 11 '20 at 19:21
  • @BalabhadraChand you're welcome, you can further thank me by [accepting the answer](https://meta.stackexchange.com/a/5235/684852) – anastaciu May 11 '20 at 19:23
  • One more thing if I give some initial value to a (let a=3), It shows the warning of " non-static data member initializers only available with -std=c++11 or -std=gnu++11" .So how to avoid it? – Rev99 May 11 '20 at 19:24
  • @BalabhadraChand, this means your compiler does not use C++11, it uses an older standard, how do you compile your code? Do you use a specific program? – anastaciu May 11 '20 at 19:26
  • I am using dev c++ version 5.11. Any suggestion on a better IDE than Dev c++? – Rev99 May 11 '20 at 19:36
  • 1
    @BalabhadraChand, it's not bad, it's a bit old, if you want to continue to use it with C++ 11 options, you can do it, [check out this post](https://stackoverflow.com/q/16951376/6865932), there are many others like Intel C++, Visual Studio, C++ Netbeans, etc, I wouldn't say one is better than the others, it's a matter of personal choice. [This page](https://alternativeto.net/software/orwell-dev-c-/) has some possible replacements, you might check it out – anastaciu May 11 '20 at 19:50
  • 2
    You can probably get by with Dev C++ if you like it (I do a lot of coding with an editor I bought in 1998, so I am in poor moral position complain about using old tools), but you should consider pointing at a more recent compiler. If you follow the instructions outlined in [How to install MinGW-w64 and MSYS2?](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2) you'll get an up-to-date compiler and an impressive ecosystem of extra tools and libraries. After that you need to consult Dev C++'s documentation on how to instruct it to use a different compiler. – user4581301 May 11 '20 at 20:30