0

Trying to learn C++ and hitting a wall with a few things. Would appreciate some pointers from the C++ experts on explaining on what is happen ing under the hood.

My BigNum class is below. My questions are

  1. BigNum b1 = BigNum(2, {2, 8}); does not work Error: No matching constructor for initialization of 'BigNum'
    • But int arr[] = {2, 8}; BigNum b1 = BigNum(2, arr); works
  2. I also see some compiler warnings for all constructors below. E.g:Candidate constructor not viable: requires 0 arguments, but 2 were provided for the default constructor below
  3. Is there a one-line way to initialize the private members in the constructor. E.g:
    • BigNum(int numDigits, int digits[]): _n(numDigits), _digits(digits) {};
    • Or even better BigNum(int numDigits = 0, int digits[] = {}): _n(numDigits), _digits(digits) {};

class BigNum {
    int _n = 0;
  int _digits[MAX_DIGITS] = {};
public:
  BigNum() {};
  BigNum(int numDigits, int digits[]) {
    if (numDigits >= MAX_DIGITS || numDigits < 0) {
      return;
    }
    _n = numDigits;
    memcpy(_digits, digits, _n * sizeof(_digits[0]));
  };
  BigNum(const BigNum &bigNum) {
    _n = bigNum._n;
    memcpy(_digits, bigNum._digits, _n * sizeof(_digits[0]));
  };
};

5
  • 4
    1. You likely want a constructor taking std::initializer_list. With such a constructor, you could do BigNum({2, 8}) 2. Those are not separate warnings, but further explanations for the original error. The compiler tells you what constructors it tried, and why each one was found unsuitable. Commented Aug 31, 2020 at 0:20
  • 4
    3. There would be if you use something better than a plain C array for storing your digits - e.g. std::array or std::vector. These can be initialized in the constructor initializer list, while plain array cannot. Commented Aug 31, 2020 at 0:24
  • Thanks does the std::initializer_list work if data member is an int array. Seems to only work for std::vector Commented Aug 31, 2020 at 0:28
  • Just saw your comment for 3. Thanks. yes using std::initializer_list for the param and using for loop to initialize _digits works. Can you make this the answer, so that I can accept? Commented Aug 31, 2020 at 0:33
  • 1
    You might find "BigNum() = delete;" useful. Commented Aug 31, 2020 at 2:54

1 Answer 1

3

So, to echo what Igor said in the comments, std::vector has a convenient constructor from an initializer_list, so you could do:

#include <vector>
#include <initializer_list>

class BigNum {
    std::vector <int> digits;
public:
    BigNum (int max_digits) : digits (max_digits) {}
    BigNum (std::initializer_list <int> initial_digits) : digits (initial_digits) {}
};

int main()
{
    BigNum bn ( { 1, 2, 3, 4 } );
    BigNum bn2 (5);
}

Note that we now don't need a copy constructor - the default one generated by the compiler will work just fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.