Rog Matthews
Rog Matthews

Reputation: 3247

Error in C++ code

The code is written to implement a Bit class with some common functions.

#include <iostream>
#include <math.h>
using namespace std;

class Bit
{ 
    int width;
    int value;
    public:
        Bit(int v, int w)
        {
            value=v;
            width=w;
        }
        Bit(const Bit& b)
        {
            value= b.value;
            width= b.width;
        }
        int getWidth()
        {
            return width;
        }
        int getValue()
        {
            return value;
        }
        Bit plus(int newval)
        {
            value+=newval;
            if(value>=pow(2,width))
            cout<<"Overflow";
            return this;
        }
};

The error message is :

Conversion from 'Bit* const' to non-scalar type 'Bit' requested.

How can i remove the error?

Upvotes: 0

Views: 165

Answers (6)

LihO
LihO

Reputation: 42093

Method plus(int newval) should return *this; instead of this. That's the error. Also return value of type Bit& (reference) would make more sense. Although you probably don't need to return a reference to the object that you have used to call this method (check Mike's answer).

Also note that pow(2,width) is equal to (1 << width).

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254531

this is a pointer, and your plus function declares that it returns a value.

You probably want to change the return type to void and not return anything; I can't see a good reason to return a copy of the object.

Perhaps you want to return a reference in order to chain calls:

Bit & plus(int newval)        //< added &
{
    value+=newval;
    if(value>=pow(2,width))
        cout<<"Overflow";
    return *this;             //< added *
}

now you can write:

bit.plus(1).plus(3).plus(42);

if you really want to.

Upvotes: 4

Riccardo Tramma
Riccardo Tramma

Reputation: 593

Bit plus(int newval)
{
 value+=newval;
 if(value>=pow(2,width))
 cout<<"Overflow";
 return this;
}

This methot returns a Bit but you return a pointer to Bit. You should return: return *this , or the method signature should be Bit* plus(int newval) (if you decide to return "this")

Upvotes: 0

martiert
martiert

Reputation: 741

Would be nice if you could add the line number, or just give the function where it fails. However, this seems to be where it stops.

Bit plus(int newval)
{
 value+=newval;
 if(value>=pow(2,width))
 cout<<"Overflow";
 return this;
}

The thing is, this is a pointer to the object. That means this is actually a Bit *. Since you want to return a Bit, this will create a failure as you describe. To fix this you could change it to:

Bit& plus(int newval)
{
  value+=newval;
  if(value>=pow(2,width))
    cout<<"Overflow";
  return *this;
}

Which would return a reference to the updated object. You could of course also return a Bit*, but I would try to avoid using pointers unnecessarily.

Upvotes: 1

k06a
k06a

Reputation: 18805

Bit & plus(...) { ... return *this; }

Upvotes: 1

Gobra
Gobra

Reputation: 4261

Your "plus" method returns "Bit* const". I guess whole method should be

Bit& plus(int newval)
{
 value+=newval;
 if(value>=pow(2,width))
 cout<<"Overflow";
 return *this;
}

Upvotes: 2

Related Questions