Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:06:06+00:00 2026-06-03T20:06:06+00:00

I am working on creating a class to handle very large numbers for use

  • 0

I am working on creating a class to handle very large numbers for use in Project Euler challenges. I have overloaded both << and + to work with the class. The following compiles and works properly:

BigNum a(400000000);
BigNum b(400000000);
BigNum c;

c = a + b;

cout << c; 

The following produces a compile error:

BigNum a(400000000);
BigNum b(400000000);

cout << (a + b); 

The error is: error: no match for ‘operator<<‘ in ‘std::cout << BigNum::operator+(const BigNum&) const(((const BigNum&)((const BigNum*)(& b))))’

For reference, here is the class definition and implementation:

/* BigNum.h */

#include <iostream>
#include <vector>

class BigNum {
public: 
BigNum();
BigNum(const BigNum &src);
BigNum(int num);
void print(std::ostream *os);

public:
BigNum &operator=(const BigNum &src);
BigNum &operator+=(const BigNum &src);
const BigNum operator+(const BigNum &src) const;

private:
bool negative;
std::vector<int> digits;
};

std::ostream &operator<<(std::ostream &os, BigNum &bNum);

/* BigNum.cpp */

#include "BigNum.h"

BigNum::BigNum() {
digits.push_back(0);
}

BigNum::BigNum(const BigNum &src) {
digits = src.digits;
}

BigNum::BigNum(int num) {
if(num == 0) {
    digits.push_back(0);
}
else {
    while(num != 0) {
        digits.push_back(num % 10);
        num /= 10;
    }
}
}

void BigNum::print(std::ostream *os) {
int i;

for(i = digits.size() - 1; i >= 0; i--) {
    *os << digits[i];
}
}

std::ostream &operator<<(std::ostream &os, BigNum &bNum) {
bNum.print(&os);
return os;
}

BigNum & BigNum::operator=(const BigNum &src) {
if(this == &src) return *this;

digits = src.digits;

return *this;
}

BigNum & BigNum::operator+=(const BigNum &src) {
int carry, i, k;

carry = 0;

if(digits.size() < src.digits.size()) {
    digits.resize(src.digits.size(), 0);
}

{
    k = src.digits.size();
    for(i = 0; i < k; i++) {
        digits[i] += src.digits[i]; 
        digits[i] += carry;
        carry = 0;
        if(digits[i] > 9) {
            carry = 1;
            digits[i] -= 10;
        }
    }

    k = digits.size();
    for(i = src.digits.size(); i < k; i++) {
        digits[i] += carry;
        carry = 0;
        if(digits[i] > 9) {
            carry = 1;
            digits[i] -= 10;
        }
    }

    if(carry) {
        digits.resize(digits.size() + 1, 1);
    }
}

return *this;
}

const BigNum BigNum::operator+(const BigNum &src) const {
BigNum result(*this);
result += src;
return result;
}

Why does

cout << (a + b);

not work? There are obvious work-arounds, but I want to understand why this is happening. I am new to C++, so if there is anything else I am doing incorrectly or could be doing better, please let me know. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:06:07+00:00Added an answer on June 3, 2026 at 8:06 pm

    Your overload of operator<< doesn’t take the const BigNum that operator+ returns. Change the parameter type in operator<< to const BigNum &.

    The function thinks it’s allowed to modify the object passed in, but you’re passing a non-modifiable object, so it doesn’t match.

    As pointed out below, the print() function you call from there needs to be const as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on creating a Java class to map an XML file that
I am creating a custom module in magento.I have created block class and phtml
I am working on creating a progress bar for ffmpeg in java. So for
I'm working on creating a windows service that will send emails to a customer
I am working on creating a blog with ASP.Net 4, MVC 3, Razor and
I'm working on creating a map of Day Z (the game) with leaflet JS
I'm currently working on creating a Download Manager for Android. In order to optimize
I'm working on creating a self updating application and one issue I'm running into
I am working on creating an MVC application for an existing Bug tracker of
I am working on creating a thread safe control for my windows forms application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.