-2

I'm new to C++

below is the code for converting object of english distance(feet' inches") to meters and vice versa

#include <iostream>
using namespace std;

class Distance
{
private:
    const float MTF;
    int feet;
    float inches;
public:
    Distance() : feet(0), inches(0.0), MTF(3.280833F) //no argument constructor
    { }
    Distance(float meters) : MTF(3.28033F)//(1-arg constructor)
    {//coverting metres to distance object
        float fltfeet = MTF * meters;
        feet = int(fltfeet);
        inches = 12*(fltfeet-feet);
    }
    Distance(int ft, float in) : feet(ft), inches(in), MTF(3.280833F)
    { }
    void getdist()//get distance from user
    {
        cout << "\nEnter feet: "; cin >> feet;
        cout << "Enter inches: "; cin >> inches;
    }
    void showdist() const // o/p the distance 
    { cout << feet << "\'-" << inches << '\"'; }
    operator float() const //conversion operator
    { // converts distance to meters
        float fracfeet = inches/12;
        fracfeet += static_cast<float>(feet);
        return fracfeet/MTF;
    }
};
int main()
{
    float mtrs;
    Distance dist1 = 2.35F; //meters to distance
    cout << "\ndist1 = "; dist1.showdist();
    mtrs = static_cast<float>(dist1); //casting distance to meters

    cout << "\ndist1 = " << mtrs << " meters\n";
    Distance dist2(5, 10.25);
    mtrs = dist2; //casting dist2 to meters
    cout << "\ndist2 = " << mtrs << " meters\n";


    Distance dist3; //new object dist3
    dist3 = mtrs; //here is the error 


//not converting meters to distance object
   

    cout<<"\ndist3 = ";dist3.showdist();
    return 0;
}

but the code shows the error :

In member function 'Distance& Distance::operator=(const Distance&)':

error: non-static const member 'const float Distance::MTF', can't use default assignment operator

should'nt it be converting mtrs to object dist3 ?

why error occurs?

Community
  • 1
  • 1
  • Please edit your question to contain a [mcve]. Error shown is generated from different source code than you provided. – Slava Sep 23 '16 at 17:32
  • Possible duplicate of [assignment of class with const member](http://stackoverflow.com/questions/11601998/assignment-of-class-with-const-member) – Ami Tavory Sep 23 '16 at 17:36
  • This isn't relevant to the problem you're having, but you've set MTF to different values in your `Distance(float meters)` constructor as compared to your `Distance()` and `Distance(int ft, float in)` constructors. This is one reason whey Slava's answer below is nice when you can use c++11. – Jvinniec Sep 23 '16 at 17:57
  • "English distance"? In England we've been using metres since 1969. That's 47 years. Almost half a century! – Lightness Races in Orbit Sep 23 '16 at 18:07

6 Answers6

3

You error is actually with the line

dist3 = mtrs;

not

Distance dist3;

The reason for this is Distance has a const member variable. Since it is const it cannot be assigned to which cause the default copy assignment and move assignment operators to be deleted.

You are going to have to rethink your design if you want to allow assignment of your objects or write your own custom assignment functions.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

If you want to have class scope constant, you better change your class definition to:

class Distance
{
private:
    static constexpr float MTF = 3.280833F;
    int feet;
    float inches;
public:
    Distance() : feet(0), inches(0.0)
...

This will eliminate error you get and will work as you intended. Plus you do not have to define that constant value multiple times as you have in your code.

Note: if you cannot use C++11 you can make MTF global constant (better in unnamed namespace inside cpp file) or just static member. Either way it will eliminate the error and you will need to define it only once, which is less error prone.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • Just a note, you'll need to compile with c++11 flags (i.e. `std=c++11`) to make this solution work. Otherwise you'll get warnings or errors at compile time. – Jvinniec Sep 23 '16 at 17:50
1

You have to override assignment operator. And the code should be as shown below

    #include <iostream>
using namespace std;

class Distance
{
private:
    const float MTF;
    int feet;
    float inches;
public:
    Distance() : feet(0), inches(0.0), MTF(3.280833F)
    { }
    Distance(float meters) : MTF(3.28033F)
    {
        float fltfeet = MTF * meters;
        feet = int(fltfeet);
        inches = 12*(fltfeet-feet);
    }
    Distance(int ft, float in) : feet(ft), inches(in), MTF(3.280833F)
    { }
    void getdist()
    {
        cout << "\nEnter feet: "; cin >> feet;
        cout << "Enter inches: "; cin >> inches;
    }
    void showdist() const
    { cout << feet << "\'-" << inches << '\"'; }
    operator float() const
    {
        float fracfeet = inches/12;
        fracfeet += static_cast<float>(feet);
        return fracfeet/MTF;
    }

    Distance& operator=(const Distance & otherD)
    {
        feet = otherD.feet;
        inches = otherD.inches;

        return *this;
    }
};
int main()
{
    float mtrs;
    Distance dist1 = 2.35F;
    cout << "\ndist1 = "; dist1.showdist();
    mtrs = static_cast<float>(dist1);

    cout << "\ndist1 = " << mtrs << " meters\n";
    Distance dist2(5, 10.25);
    mtrs = dist2;
    cout << "\ndist2 = " << mtrs << " meters\n";
    Distance dist3; //here is the error 
    dist3 = (Distance)mtrs ;


    //cout<<"\ndist3 = ";dist3.showdist();
    return 0;
}

Like another user said, you have a "const" variable, so you have to override assigment operator to address your requirements.

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
0

I gather that the error comes at the line

dist3 = mtrs;

The problem here is that Distance does not have an assignment operator that takes an argument of type float, so the compiler tries to create a temporary object of type Distance and construct it with the argument mtrs; that's okay, but the next step is to assign that temporary value to dist3, and the compiler is complaining that it can't assign the value of MTF in the temporary object to the value of MTF in dist3 because MTF is const.

Because of that const, objects of type Distance cannot be assigned. So, for example, dist3 = dist2 would also fail. That's probably not what you intended, and you can fix this by adding an assignment operator that simply ignores the value of MTF.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

The error occurs, because you could not declare const inside a class. You should define the const variable outside the class. You should replace const float MTF with float MTF here.

Bhoke
  • 468
  • 6
  • 22
0

As stated in other answers, the issue is that you have declared the MTF variable as const. There are ways around this though. You've set the variable to be const because it's a constant and shouldnt change. Instead, add a dedicated Distance& operator=(float feet) Method in which you actually set the feet and inches variable when passed in a float value:

class Distance
{
private:
    /* ... */
public:
    /* ... */
    Distance& operator=(float feet)
    {
        // Set the feet and inches here from
        // the passed feet variable
        return *this;
    }
};

That should solve the problem of assigning the variable from a float.

Jvinniec
  • 616
  • 12
  • 18