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 ];
}
#include <array>