1

I'm working on a problem where I'm to build a class with 40 integer array to calculate addition and subtraction on 40 digit numbers.

I've written below code, but for some reason, it's keep failing with an error message:

Implicit instantiation of undefined template 'std::_1::array<int, 40>'.

I don't understand the error message and do not see any issue with the code. Could you please help?

Also, as you can see, I'm using memcpy function, but when I just enter std::memcpy( hugeInteger, integer, sizeof( integer ) ), it spits out No viable conversion from 'std::array<int, 40>' to 'void'. As I understand, memcpy accepts pointer, and hugeInteger is a pointer to the first element of an array. Am I not understanding correctly?

Below is the header:

#ifndef __Chapter_9__HugeInteger__
#define __Chapter_9__HugeInteger__

#include <stdio.h>
#include <vector>

class HugeInteger
{
public:
    explicit HugeInteger( bool = 1, std::array< int, 40 > = {} );
    void setHugeInteger( std::array< int, 40 > );
    void print();

private:
    bool checkHugeInteger( std::array< int, 40 > );

    std::array< int, 40 > hugeInteger = {};
    bool sign;
};

#endif /* defined(__Chapter_9__HugeInteger__) */

Below is the cpp:

#include <array>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "HugeInteger.h"

HugeInteger::HugeInteger( bool sign, std::array< int, 40 > integer )
: sign(sign)
{
    setHugeInteger( integer );
}

void HugeInteger::setHugeInteger( std::array< int, 40 > integer )
{
    if ( checkHugeInteger( integer ) )
        std::memcpy( hugeInteger, integer, sizeof( integer ) );
    else
        throw std::invalid_argument( "Single digit in the huge integer may not be negative ");
}

bool HugeInteger::checkHugeInteger( std::array< int, 40 > integer )
{
    bool tester = 1;

    for ( int i = int( integer.size() ) - 1; i >= 0; i-- )
    {
        if ( integer[i] < 0 )
        {
            tester = 0;
            break;
        }
    }

    return tester;
}

void HugeInteger::print()
{
    if ( sign < 0 )
        std::cout << "-";

    for( int i = int( hugeInteger.size() ) - 1; i >= 0; i-- )
        std::cout << hugeInteger[ i ];
}
6
  • 3
    The header should have #include <array> Commented Mar 22, 2015 at 23:36
  • I'd pass the array by const reference to avoid copying. Commented Mar 22, 2015 at 23:42
  • @McNabb: #include <array> did not solve the problem ;( Commented Mar 23, 2015 at 1:11
  • @zenith: I'm trying to initialize 'hugeInteger.' That is why I'm assigning '= {}' to the array. If I do 'explicit HugeInteger( bool = 1, std::array< int, 40 >& = {} );,' wouldn't this assign the reference itself to {}? Commented Mar 23, 2015 at 1:12
  • @McNabb: NVM! It actually did! Yay~! ... Now I feel so dumb... This took me 2 hours... Commented Mar 23, 2015 at 1:19

1 Answer 1

4

I don't get the error you mention, using your exact code. However your code does not have a main function. So I presume there is another .cpp file that you haven't mentioned which contains main. If so, the error could be because that file includes HugeInteger.h without doing #include <array>.

The HugeInteger.h should do #include <array> itself, instead of relying on other includers to do it.

To fix the memcpy problem, replace the memcpy line with:

hugeInteger = integer;

The std::array container has value semantics, so you can assign it.

I'd also recommend changing your functions which accept an array to take it by const reference. This will avoid unnecessary copies when functions are called. Also, the checkHugeInteger function could be static as it does not use any member variables of the class.

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

5 Comments

Thank you for your response and the tips. By the way, if I were to use memcpy, what am I doing wrong? Is there any problem with my memcpy statement?
Oh by the way, the main function only has two lines: HugeInteger integer; integer.print();
@GrinNare Yes the memcpy statement is wrong. memcpy needs two pointers as parameters, you're passing std::array.
@zenith: (Correct me if I'm wrong here) As I understand if... int b[5]; int *bPtr; Then... bPtr == &b[0] == b, which means the array name 'b' itself is a point, no?
@GrinNare C-style arrays decay to a pointer to the first element, however std::array does not (which is one of the main reasons for std::array existing in the first place)

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.