Hey All

I am testing myself with making a big number class. Right now I'm just working with integers but I hope to expand it to floats as well. I am using a vector of integers for the container and i have run into a problem with my addition routine. I'm testing to see if one number is a negative or not and then calling the appropriate minus function but I am getting an error when trying to pass the this pointer to the function. The error I'm getting is:
error C2662: 'Number::operator -' : cannot convert 'this' pointer from 'const Number' to 'Number &'

here is my code that i have so far

Number.h

// inclusion guarding
#ifndef NUMBERCLASS_H
#define NUMBERCLASS_H

#include <string>
#include <vector>

class Number
{
public:
	// constructors
	Number();
	Number(int);
	Number(unsigned int);
	Number(char *);
	Number(std::string);

	// destructor
	~Number() {};

	// get functions
	int GetNumberAsInt();			// return as a int if possible otherwise returns 0 and flags error
	std::string GetNumber();
	bool GetSign();					// reuturns true if negative
	bool GetToBig();				// returns true if GetNumberAsInt fails
	
	// math operators
	Number & operator=(const Number &);
	Number & operator+(const Number &);
	Number & operator-(const Number &);

private:
	// private functions
	char* makeInt(char *);
	// private data
	std::vector<int> data;
	int length;
	bool sign;
	bool toBig;
};

#endif
Number.cpp operator+ function

Number & Number::operator +(const Number & number)
{
	int carry = 0;
	int size = 0;
	if (sign && !number.sign)
	{
		return number.operator -(*this);  // this is where I'm getting my error
	}
	if (number.sign && !sign)
	{
		return this->operator -(number);
	}
	std::vector<int>::const_iterator firstIt;
	std::vector<int>::const_iterator secondIt;
	std::vector<int> temp;
	if (number.length > length)
	{
		firstIt = data.begin();
		secondIt = number.data.begin();
	}
	else
	{
		firstIt = number.data.begin();
		secondIt = data.begin();
	}
	while (*firstIt)
	{
		carry = carry + *firstIt + *secondIt;
		if (carry >= 10)
		{
			temp.push_back(carry - 10);
			if (carry == 10)
				carry = 1;
			else
				carry -= 10;
		}
		else
		{
			temp.push_back(carry);
			carry = 0;
		}
		firstIt++;
		secondIt++;
	}
	while (*secondIt)
	{
		if (carry > 0)
		{
			carry = carry + *secondIt;
			if (carry >= 10)
			{
				temp.push_back(carry - 10);
				if (carry == 10)
					carry = 1;
				else
					carry -= 10;
			}
			else
			{
				temp.push_back(carry);
				carry = 0;
			}
		}
		else
		{
			temp.push_back(*secondIt);
		}
		secondIt++;
	}
	data = temp;
	return *this;
}

Any help would be appreciated. Any other advice would also be appreciated.

Recommended Answers

All 5 Replies

>>
error C2662: 'Number::operator -' : cannot convert 'this' pointer from 'const Number' to 'Number &'

That means you are passing a const object into a non-const function.
Const-correct your functions, that is do something like this if you can :

Numbers operator+(const Number& num)const;

if i were to make my function const then i would need to create a object of my number class in my function and then return that since in my function i am returning a Number & ?

The problem is that the parameter number is const (constant) but you are trying to use it to call operator- which is not a const member function. number (const Number &) can not be converted to Number * in order to make the method call in return number.operator -(*this); and you get that error because of it.

However I think you have a more fundamental error because operator+ (and operator-) should not return *this, it should return a new object there should be no in-place alteration of the current object(this) under addition.

For example

int a = 5;
int b = 10;
int c;
c = a + b;

After running this code a = 5, b = 10 and c = 15 but

Number a = 5;
Number b = 10;
Number c;
c = a + b;

After running this code the way you have it written a = 15, b = 10 and c = 15 because Number::operator+ modifies the object it is called on.

Instead of creating operator+ I suggest you create operator+= also you should implement a copy constructor Number::Number(const Number&).

Then you can simply implement operator+ as

Number Number::operator+(const Number& rhs)
{
    return Number(*this) += rhs;
}

implement it inline and if you are lucky you will get a return code optimisation.

if i were to make my function const then i would need to create a object of my number class in my function and then return that since in my function i am returning a Number & ?

All the non assigning operators, such as operator+ should return an object not a reference if they are to work correctly and handle memory correctly.

okay that i believe is my problem i should not modify my data in the Number object but return a number object which is the sum of the 2 objects. thanks for the help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.