Arbitrary Long numbers addition

function not declared within scope.

Page 1 of 1

10 Replies - 661 Views - Last Post: 08 April 2010 - 05:24 AM Rate Topic: -----

#1 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Arbitrary Long numbers addition

Posted 08 April 2010 - 04:32 AM

Hey people, i'm making a program that allows the user to add arbitrary long numbers. At the moment, i am having trouble with compiling what code i have. The error message i am getting is:
"
Bigint.cpp: In Function 'Bigint operator+(const Bigint&, const Bigint&)':
Bigint.cpp:88: error: 'addition' was not declared in this scope
"

Can anyone please offer assistance as to why addition cannot be accessed?
also, if you see anything else wrong with the code which may be of usefulness to me, please say :)

thankyou!

#include "Bigint.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;

void Bigint::sToArray(Bigint& n){ // sets array elements to 0, then right aligns 
	int length_;		  // the input number within the array
	for(int i = 0; i < 256; i ++){
		n.num[i]=0;
	}
	n.length_  = n.text.length();
	for(int i = 255; i >= 255-n.length_ ; i --){
		n.num[i] = n.text[i] - '0';
	}
}

string Bigint::getResult(Bigint& n){
	return n.result;
}

void Bigint::arrayToS(Bigint& n){ //turns an array to a string
	string result;
	ostringstream ss;
	
	for(int i = 0; i < 256 ; i++){
		if(n.answer[i] != 0 || n.answer[i] == 0 && n.answer[i-1] != 0){
			ss << n.answer[i];
		}
	}
	
	n.result = ss.str();	
}

Bigint Bigint::addition(Bigint& n1, Bigint& n2){
	Bigint res("");
	int tempAns;
	
	for(int i = 255; i >= 0; i--) {
		
		res.tempAns = n1.num[i] + n2.num[i] + res.carry;
		if(res.tempAns>9){
			res.carry = 1;
			res.tempAns = res.tempAns%10;
		}else{
			res.carry = 0;
		}
		res.answer[i] = res.tempAns;
	}
	
	arrayToS(res);
	
	return res;
}

Bigint::Bigint(string s){
	
	text = s;
	
}

Bigint operator +(const Bigint& n1, const Bigint& n2){
	Bigint temp = addition(n1, n2);
	return temp;
}

ostream& operator <<(std::ostream& out, const Bigint& n){
	string text;
	out << n.text;
	return out;
}

istream& operator >>(std::istream& in, Bigint& n){
	string text;
	in >> n.text;
	return in;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Arbitrary Long numbers addition

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:39 AM

Bigint operator +(const Bigint& n1, const Bigint& n2){


You didn't tell the Compiler that Bigint is a member function of your class. Try this:
Bigint Bigint::operator +(const Bigint& n1, const Bigint& n2){


Was This Post Helpful? 0
  • +
  • -

#3 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:42 AM

Thankyou for the quick response,
yes, i have tried doing it that way also, however then it states that the "operator+" function can only take one or zero parameters.
Was This Post Helpful? 0
  • +
  • -

#4 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 100
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:43 AM

Hello sarmanu,

Please include your Bigint.h file code.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:45 AM

View PostElcric, on 08 April 2010 - 03:43 AM, said:

Hello sarmanu,

Please include your Bigint.h file code.

Thanks


haha, its Chatterbox not sarmanu :)

bigint.h is:

#ifndef BIGINT_H_
#define BIGINT_H_

#include <iostream>

using namespace std;

class Bigint {
public:
	void sToArray(Bigint& n);
	string getResult(Bigint& n);
	void arrayToS(Bigint& n);
	Bigint addition(Bigint& n1, Bigint& n2);
	
	Bigint(std::string str);
	
	friend Bigint operator +(const Bigint& n1, const Bigint& n2);
	friend Bigint operator -(const Bigint& n1, const Bigint& n2);
	friend Bigint operator *(const Bigint& n1, const Bigint& n2);
	friend Bigint operator /(const Bigint& n1, const Bigint& n2);
	
	friend std::ostream& operator<< (std::ostream& out, const Bigint& n);
	friend std::istream& operator>> (std::istream& in, Bigint& n);
	
private:
	int num[256];
	int answer[256];
	string text;
	string result;
	int carry;
	int tempAns;
	int length_;
};

#endif


This post has been edited by Chatterbox: 08 April 2010 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:48 AM

That would be logical. You should know that every member function of your class, has a hidden parameter, the "this" pointer. Example:
class X
{
public:
   // You may think that member_func has no parameters, but in fact
   // it has, and it would be "X *const this"
   void member_func()
   {
       // do something
   }
};


So, your operator + already has two parameters. Rethink your logic.
operator overloading

This post has been edited by sarmanu: 08 April 2010 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:58 AM

I think i kind of understand what you mean, but i'm not sure on how to continue. i am very new to c++ and this and am already stretched to the boundaries of my knowledge. my lecturer didn't explain well at all how to overload operators and interaction between classes.
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 04:58 AM

First of all, you need to add a default constructor to your class. That would be:
// default constructor takes no parameters
Bigint() 
{
   // initialize variables with some default values
   // or simply don't do anything!
}


Secondly, you shouldn't declare the +,/,-,* operators as friends. It makes no sense. And for your question, you may try this:
// operator + can only take one parameter. n1 is not const, because
// your addition member function does not take any const parameters.
// We could try a const_cast, but there's no need, as you are a beginner.
Bigint Bigint::operator +(Bigint& n1) {
        // *this represents the class itself
        Bigint temp = addition(*this, n1);
        return temp;
}


This post has been edited by sarmanu: 08 April 2010 - 05:00 AM

Was This Post Helpful? 1
  • +
  • -

#9 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 05:19 AM

Well that is all compiling now,
but now it cannot find the addition function.
it is saying:
Bigint.cpp:95: error: no matching function for call to 'Bigint::addition(Bigint&, const Bigint&)'
Bigint.cpp:37: note: candidates are: Bigint Bigint::addition(Bigint&, Bigint&)

any ideas? :S
Was This Post Helpful? 0
  • +
  • -

#10 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 05:24 AM

well, i got it to compile for now :) lets see if i can actually get this operator to work :S
thankyou for your help! i may be back haha :D
Was This Post Helpful? 0
  • +
  • -

#11 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Arbitrary Long numbers addition

Posted 08 April 2010 - 05:24 AM

EDIT: didn't see your edited post :P

This post has been edited by sarmanu: 08 April 2010 - 05:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1