Hello. I am redoing a program I made that performs arithmetic with large integers. I am somewhat new to dealing with classes and this is suppose to help me learn how to use them.
There are two classes so far: largeInt, which as the name implies represent any number. I also created a class called buffer that is composed of 3 largerInt's.
The program files are:
large_int_4.0.cpp --- main function
largeInt.h --- largeInt class
largeIntimpl.cpp --- largerInt code
buffer.h --- buffer class
bufferImpl.cpp --- buffer code
Everything complides and works as designed until I put the following code that creates a buffer object in main. I get the link error mentioned in the title:
1 2
|
buffer MyBuff(Number1, Number2); //Number1 & Number2 are largeInt's
MyBuff.print();
|
So then I tried using #ifndef/#endifs to guard against loading headers more thant once. That has not worked.
I then decided to condense my files and see if the program would work. I put both class header files into one header file and both class code files into one file. It works that way but I don't see the big difference and I don't want to keep it like that.
Any comments/constructive critisims would be appreciated. However, I must reiterate that my program works when I condense everything into 3 files. I just need it to work with 5 files. And I envision making more class files in the future so I need my code to link properly. Comments of the nature of why am I doing something that way that do not address the linking problem will most likely be ignored.
large_int_4.0.cpp (main)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#pragma region Include
#include "stdafx.h"
#include "largeInt.h"
#include "buffer.h"
#include <iostream>
#include <string>
using namespace std;
bool is_number(string S); //Checks if number is entered
void end_program();
#pragma endregion
int main()
{
string input_number1, input_number2;
bool done=false;
system("cls");
cout << "\n C++ Large Integer Arithmetic\n\n";
while ( !done )
{
bool input_done=false;
while ( !input_done )
{
cout << "\nEnter Number1. (Or anything else to exit): ";
cin >> input_number1;
if ( is_number(input_number1) == 0 ) end_program();
cout << "\nEnter Number2: ";
cin >> input_number2;
if ( is_number(input_number2) == 0 ) end_program();
input_done=true;
}
largeInt Number1(input_number1);
largeInt Number2(input_number2);
Number1.print();
Number2.print();
buffer MyBuff(Number1, Number2);
MyBuff.print();
} //end main while loop
}
bool is_number(string S)
{
//return 1 if string is a number and 0 if not
if ( S.at(0) != '-' && isdigit(S.at(0)) == 0 ) return 0;
//first char must either be '-' or digit
//(isdigit returns 0 if not a digit)
int length_longest = S.length();
for (int j=1; j < length_longest; j++)
{
if ( isdigit(S.at(j)) == 0 ) return 0;
}
return 1;
}
void end_program()
{
cout << endl << "Bye Bye." << endl << endl;
system("pause");
exit(0);
}
|
largeInt.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
//largeint.h, the specification file for the class LargeInt
#pragma region Include
#ifndef _LARGEINT_INCLUDED
#define _LARGEINT_INCLUDED
#include <string>
using namespace std;
#pragma endregion
class largeInt
{
public:
void print() const;
//Fuction that outputs *number array, reading right to left
largeInt(string S);
//constructor for string input
//Preconditions: string must be postive or negative number
// -test input in main
largeInt();
//default constructor
int *Number1;
int Length_Num1;
bool IsNeg_Num1;
private:
};
|
largeIntImpl.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
//Inplementation source for largeInt class
#pragma region Declarations
#include "stdafx.h"
#include "largeInt.h"
#include <iostream>
#include <sstream>
using namespace std;
#pragma endregion
void largeInt::print() const
{
stringstream sstr1;
for (int k = ( Length_Num1 - 1 ); k >=0; k--)
//length is largeInt variable
//read indexes from right to left
{
sstr1 << Number1[k];
}
cout << "\n\n" << sstr1.str();
}
largeInt::largeInt(string S)
{
Length_Num1 = S.length(); //largeInt variable
Number1 = new int[Length_Num1];
for (int j=0; j < Length_Num1; j++)
//intialize array to 0
{
Number1[j] = 0;
}
if ( S.at(0) == '-' )
//check for negative sign
{
IsNeg_Num1 = true;
S.erase(0,1);
//erase 1 char at position 1 (remove negative sign)
}
else { IsNeg_Num1 = false; }
char digit_char;
int digit_int;
//stores single char of string, as char and int
int col_count=0;
//because for loop below is decremented, this is incremented for array index
for ( int j = ( S.length() - 1 ) ; j >= 0 ; j-- )
//reads last digit of string first then stores in first index of array
{
digit_char=S.at(j);
digit_int=digit_char - '0';
//ASCII Math;
//atoi() might be better but I always have trouble with it
//would need to redo input strings as char arrays?
Number1[col_count] = digit_int;
col_count++;
}
}
largeInt::largeInt()
//default constructor
{
Number1 = NULL;
Length_Num1 = 0;
IsNeg_Num1 = false;
}
|
buffer.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
//buffer.h, the specification file for the class buffer (composition of LargeInt's)
#pragma region Include
#ifndef _BUFFER_INCLUDED
#define _BUFFER_INCLUDED
using namespace std;
#pragma endregion
class buffer
{
public:
void print() const;
//Function that prints Number1, Number2, and Calc
buffer(largeInt A, largeInt B);
//contructor for 2 largeInt's
buffer();
//default constructor
private:
largeInt Number1;
largeInt Number2;
largeInt Calc;
};
#endif _BUFFER_INCLUDED
|
bufferImpl.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
//Inplementation source for buffer class
#pragma region Declarations
#include "stdafx.h"
#include "largeInt.h"
#include "buffer.h"
#include <iostream>
using namespace std;
#pragma endregion
void buffer::print() const
{
Number1.print();
Number2.print();
Calc.print();
}
buffer::buffer(largeInt A, largeInt B)
{
Number1 = A;
Number2 = B;
}
buffer::buffer()
//default constructor
{
}
|