Author Topic: Help with compiling this stupid C++ thing  (Read 2627 times)

0 Members and 1 Guest are viewing this topic.

Dreyth

  • Hero Member
  • *****
  • Posts: 2776
  • Respect: +821
    • View Profile
    • Email
Help with compiling this stupid C++ thing
« on: February 13, 2013, 12:24:02 pm »
0
Ok so thanks for the help in the other thread guys. I found out that I only need the multiplication algorithm if I want extra credit lol.

For now, I just want to be able to compile:
g++ HugeInteger.cpp driver.cpp

However, I'm getting an error:

HugeInteger.cpp: In member function 'HugeInteger HugeInteger::add(const HugeInteger&)':
HugeInteger.cpp:160:2: error: 'result' was not declared in this scope
HugeInteger.cpp:175:12: error: 'result' was not declared in this scope

Do I have to swap every 'array' i see with a 'result' instead or something? Not sure what to do exactly.

HugeInteger.h
Code: [Select]
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

class HugeInteger
{
    friend ostream& operator << (ostream&, const HugeInteger&);
    friend istream& operator >> (istream&, HugeInteger&);

public:

// Constructors:
    HugeInteger();
    HugeInteger(int [], int);
// I/O functions:
    HugeInteger input (char);
    HugeInteger print();
// Adding and multiplying functions:
    HugeInteger add(const HugeInteger&);
    HugeInteger multiply(const HugeInteger&);
// Operator overloading:
    bool operator > (const HugeInteger&)const;
    bool operator < (const HugeInteger&)const;
    bool operator == (const HugeInteger&)const;
    bool operator != (const HugeInteger&)const;
    HugeInteger operator + (const HugeInteger&);
    HugeInteger operator * (const HugeInteger&);

private:

    const static int maxSize = 40;
    int array[maxSize + 1];    // Watch out for fencepost error
    int size;
};
#endif

HugeInteger.cpp
Code: [Select]
#include "HugeInteger.h"

using namespace std;

// Default constructor
HugeInteger::HugeInteger(){

    for (int i = 0; i < maxSize; i++)
array[i] = 0;
}

// Copy constructor
HugeInteger::HugeInteger(int arr[], int b):size(b){

    // Make sure array size <=40
    if (size > maxSize)
size = maxSize;
    // Copy contents of array into arr
    for (int i = 0; i < size; i++)
array[i] = arr[i];
    // Fill up rest of array with 0's
    for (int i = size; i < maxSize; i++ )
array[i] = 0;

    //Take care of fencepost errors for integers
    for (int j = maxSize - 1; j >= maxSize - size; j--)
array[j] = array[j - maxSize + size];
    for (int k = 0; k < maxSize - size; k++)
array[k] = 0;
}

// This is the input function
HugeInteger HugeInteger::input(char terminal){
 
    char a;
 
    //Take characters and change into integers with atoi
    while ((a = cin.get()) != terminal){
if (a >= '0' && a <= '9'){
    array[size] = atoi(&a);
    size++;
}
    }

    //Take care of fencepost errors for integers
    for (int h = maxSize - 1; h >= maxSize - size; h--)
array[h] = array[h - maxSize + size];
    for (int j = 0; j < maxSize - size; j++)
array[j] = 0;
       
}

// This is the print function to output to user
HugeInteger HugeInteger::print(){

    bool term = false;
    int count;

    //Take out all the extra 0's again
    for(int i = 0; i < maxSize; i++){
if (array[i] != 0){
    term = true;
    count = i;
    break;
}
    }   

    //output the rest of numbers which was entered by user
    for (int i = count; i < maxSize; i++)
cout<<array[i];
}

// Overload << for ostream
ostream &operator<<(ostream&output, const HugeInteger &a){

    bool term = false;
    int count;

    //Take out unnecessary 0's we filled the array with
    for(int i = 0; i < a.maxSize; i++){
if (a.array[i] != 0){
    term = true;
    count = i;
    break;
}
    }

    //And then output the rest of the array!
    for (int i = count; i < a.maxSize; i++)
output<<a.array[i];

    return output;
}

// Overload + for addition
HugeInteger HugeInteger::operator+(const HugeInteger &a){

    return add(a);
}

// Overload * for multiplication
 HugeInteger HugeInteger::operator*(const HugeInteger &a){
   
   return  multiply(a); 
}

// Overload == for equality
bool HugeInteger::operator==(const HugeInteger &a)const{

    int i = 0;

    for(int j = 0; j < maxSize; j++)
if(array[j] != a.array[j])
    i++;

    if (i == 0)
return true;
    else
return false;
}

// Overload > for greater than
bool HugeInteger::operator>(const HugeInteger &a)const{

    int count = 0;

    for(int i = 0; i < maxSize; i++)
if(array[i] > a.array[i])
    count++;

    if(count > 0)
return true;
     
    else
return false;
}

// Overload < for less than
bool HugeInteger::operator<(const HugeInteger &a)const{

    int count = 0;

    for(int i = 0; i < maxSize; i++)
if(array[i] < a.array[i])
    count++;

    if(count > 0)
return true;
    else
return false;
}

// Addition algorithm resides in here
HugeInteger HugeInteger::add(const HugeInteger &a){

    int carry = 0;  // Initialize the carry
   
    // Add digits of corresponding elements to each other
    for (int i = maxSize - 1; i >= 0; i--){
result.array[i] = array[i] + a.array[i] + carry;

// But if the digits sum to >9...
if (array[i] > 9){

    // We enter only the right digit into element...
    int right_digit = result.array[i]%10;
    result.array[i] = right_digit;

    // And we change the carry for the next element.
    carry = (array[i] - right_digit) / 10;
}
    }

    // Return the result
    return(result);      
}

driver.cpp (I'll start working on this when I can get everything to compile)
Code: [Select]
#include "HugeInteger.h"
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{

    cout << "Please enter an operand, an operation, and another "
         << "operand up to 40 digits long." << endl
         << "Example: 097823049857902945 + 092837402983423";

    return 0;

}


Once again, the error message:

HugeInteger.cpp: In member function 'HugeInteger HugeInteger::add(const HugeInteger&)':
HugeInteger.cpp:160:2: error: 'result' was not declared in this scope
HugeInteger.cpp:175:12: error: 'result' was not declared in this scope

That member function is the addition algorithm... the last bit of code in my .cpp file (should be able to find it quick).

Basically my professor just told me to put result in that addition function and just left. i know this will be extremely easy for someone to figure out, but sometimes I can do the complicated stuff and forget how to do the easy stuff (10min ago I had to google "how to declare an array...").
« Last Edit: February 13, 2013, 12:29:35 pm by Dreyth »
I'm LAKERS from The Vertical Summit

entropy

  • Hero Member
  • *****
  • Posts: 1684
  • b00m!
  • Respect: +276
    • View Profile
Re: Help with compiling this stupid C++ thing
« Reply #1 on: February 13, 2013, 12:33:55 pm »
0

HugeInteger.cpp:160:2: error: 'result' was not declared in this scope
HugeInteger.cpp:175:12: error: 'result' was not declared in this scope


Yea you haven't declared the result variable before using it. The .array looks off to me as well. Learning to read compiler errors is a useful skill you should learn! The compiler is telling you on line 160 you're using something without declaring it first.
Goals: Cutting to 6-8% bodyfat

Dreyth

  • Hero Member
  • *****
  • Posts: 2776
  • Respect: +821
    • View Profile
    • Email
Re: Help with compiling this stupid C++ thing
« Reply #2 on: February 13, 2013, 12:45:22 pm »
0
Dam that was a fast response.

Well now I've come up with this for the add function (where the error resided):

Code: [Select]
// Addition algorithm resides in here
HugeInteger HugeInteger::add(const HugeInteger &a){

    int result[maxSize + 1];
    int carry = 0;  // Initialize the carry
   
    // Add digits of corresponding elements to each other
    for (int i = maxSize - 1; i >= 0; i--){
result[i] = array[i] + a.array[i] + carry;

// But if the digits sum to >9...
if (array[i] > 9){

     // We enter only the right digit into element...
     int right_digit = result[i]%10;
     result[i] = right_digit;

     // And we change the carry for the next element.
     carry = (array[i] - right_digit) / 10;
}
    }

    // Return the result
    return(result);        
}

And now I get this error:

HugeInteger.cpp: In member function 'HugeInteger HugeInteger::add(const HugeInteger&)':
HugeInteger.cpp:176:18: error: could not convert '(int*)(& result)' from 'int*' to 'HugeInteger'

This has exactly to do with what my professor told me... at first I didn't even have "return result;" at the end of that function. I just had "return (*this)" and she told me something about objects of a class and contents and crap but I didn't understand it. Man I really need to review.
I'm LAKERS from The Vertical Summit

entropy

  • Hero Member
  • *****
  • Posts: 1684
  • b00m!
  • Respect: +276
    • View Profile
Re: Help with compiling this stupid C++ thing
« Reply #3 on: February 13, 2013, 12:52:07 pm »
0
Getting there! I'll give you a hint: you can't return an array allocated on the stack because by the time the function returns control that "object" is no longer accessible in memory.
Goals: Cutting to 6-8% bodyfat

Dreyth

  • Hero Member
  • *****
  • Posts: 2776
  • Respect: +821
    • View Profile
    • Email
Re: Help with compiling this stupid C++ thing
« Reply #4 on: February 13, 2013, 12:54:10 pm »
0
Getting there! I'll give you a hint: you can't return an array allocated on the stack because by the time the function returns control that "object" is no longer accessible in memory.

Thanks for the tips man. Really helps my understanding! I'll work on figuring this out till my professor gets here. She was supposed to be in 40min ago already...

So what you mean is I have to return an array allocated on the heap instead? I'm going to have to read up on stack vs heap again (static vs dynamic) and figure out exactly how I should code this now.
I'm LAKERS from The Vertical Summit

Dreyth

  • Hero Member
  • *****
  • Posts: 2776
  • Respect: +821
    • View Profile
    • Email
Re: Help with compiling this stupid C++ thing
« Reply #5 on: February 13, 2013, 01:22:38 pm »
0
Hmm... so I asked someone for help and they said "I can't just return the array, I have to return the object which contains that array."

Yea crap I have to read up on objects again lol. I just don't know the nomenclature... I just know the concepts.

I'm LAKERS from The Vertical Summit

Dreyth

  • Hero Member
  • *****
  • Posts: 2776
  • Respect: +821
    • View Profile
    • Email
Re: Help with compiling this stupid C++ thing
« Reply #6 on: February 13, 2013, 03:03:07 pm »
0
Got it!!!

Thanks a lot man!

Anyway, now I have one last damn error. If I just comment out the operator* overload, it all compiles. And technically I don't need to overload the * operator simply because I don't need a multiplication algorithm.

But if I were to want to do it... how would I fix this error?

/tmp/ccEiTIds.o: In function `HugeInteger::operator*(HugeInteger const&)':
HugeInteger.cpp:(.text+0x3ef): undefined reference to `HugeInteger::multiply(HugeInteger const&)'
collect2: error: ld returned 1 exit status


anddddd here are my files again:


HugeInteger.h
Code: [Select]
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

class HugeInteger
{
    friend ostream& operator << (ostream&, const HugeInteger&);
    friend istream& operator >> (istream&, HugeInteger&);

public:

// Constructors:
    HugeInteger();
    HugeInteger(int [], int);
// I/O functions:
    HugeInteger input (char);
    HugeInteger print();
// Adding and multiplying functions:
    HugeInteger add(const HugeInteger&);
    HugeInteger multiply(const HugeInteger&);
// Operator overloading:
    bool operator > (const HugeInteger&)const;
    bool operator < (const HugeInteger&)const;
    bool operator == (const HugeInteger&)const;
    bool operator != (const HugeInteger&)const;
    HugeInteger operator + (const HugeInteger&);
    HugeInteger operator * (const HugeInteger&);

private:

    const static int maxSize = 40;
    int array[maxSize + 1];    // Watch out for fencepost error
    int size;
};
#endif

HugeInteger.cpp
Code: [Select]
#include "HugeInteger.h"

using namespace std;

// Default constructor
HugeInteger::HugeInteger(){

    for (int i = 0; i < maxSize; i++)
array[i] = 0;
}

// Copy constructor
HugeInteger::HugeInteger(int arr[], int b):size(b){

    // Make sure array size <=40
    if (size > maxSize)
size = maxSize;
    // Copy contents of array into arr
    for (int i = 0; i < size; i++)
array[i] = arr[i];
    // Fill up rest of array with 0's
    for (int i = size; i < maxSize; i++ )
array[i] = 0;

    //Take care of fencepost errors for integers
    for (int j = maxSize - 1; j >= maxSize - size; j--)
array[j] = array[j - maxSize + size];
    for (int k = 0; k < maxSize - size; k++)
array[k] = 0;
}

// This is the input function
HugeInteger HugeInteger::input(char final){
 
    char a;
 
    //Take the user inputted "characters" and...
    while ((a = cin.get()) != final){
if (a >= '0' && a <= '9'){

     // Convert them into integers.
     array[size] = atoi(&a);
     size++;
}
     }

    //Take care of fencepost errors for integers
    for (int h = maxSize - 1; h >= maxSize - size; h--)
array[h] = array[h - maxSize + size];
    for (int j = 0; j < maxSize - size; j++)
array[j] = 0;
       
}

// This is the print function to output to user
HugeInteger HugeInteger::print(){

    bool term = false;
    int count;

    //Take out all the extra 0's again
    for(int i = 0; i < maxSize; i++){
if (array[i] != 0){
     term = true;
     count = i;
     break;
}
    }   

    //output the rest of numbers which was entered by user
    for (int i = count; i < maxSize; i++)
cout<<array[i];
}

// Overload << for ostream
ostream &operator<<(ostream&output, const HugeInteger &a){

    bool term = false;
    int count;

    //Take out unnecessary 0's we filled the array with
    for(int i = 0; i < a.maxSize; i++){
if (a.array[i] != 0){
     term = true;
     count = i;
     break;
}
    }

    //And then output the rest of the array!
    for (int i = count; i < a.maxSize; i++)
output<<a.array[i];

    return output;
}

// Overload + for addition
HugeInteger HugeInteger::operator+(const HugeInteger &a){

    return add(a);
}

// Overload * for multiplication
 HugeInteger HugeInteger::operator*(const HugeInteger &a){
   
   return  multiply(a); 
}

// Overload == for equality
bool HugeInteger::operator==(const HugeInteger &a)const{

    int i = 0;

    for(int j = 0; j < maxSize; j++)
if(array[j] != a.array[j])
     i++;

    if (i == 0)
return true;
    else
return false;
}

// Overload > for greater than
bool HugeInteger::operator>(const HugeInteger &a)const{

    int count = 0;

    for(int i = 0; i < maxSize; i++)
if(array[i] > a.array[i])
     count++;

    if(count > 0)
return true;
     
    else
return false;
}

// Overload < for less than
bool HugeInteger::operator<(const HugeInteger &a)const{

    int count = 0;

    for(int i = 0; i < maxSize; i++)
if(array[i] < a.array[i])
     count++;

    if(count > 0)
return true;
    else
return false;
}

// Addition algorithm resides in here
HugeInteger HugeInteger::add(const HugeInteger &a){

    HugeInteger result;
    int carry = 0;  // Initialize the carry
 
    // Add digits of corresponding elements to each other
    for (int i = maxSize - 1; i >= 0; i--){
result.array[i] = array[i] + a.array[i] + carry;

// But if the digits sum to >9...
if (result.array[i] > 9){

     // We enter only the right digit into element...
     int right_digit = result.array[i]%10;
     result.array[i] = right_digit;

     // And we change the carry for the next element.
     carry = (array[i] - right_digit) / 10;
}
    }

    return(result);        
}

driver.cpp
Code: [Select]
#include "HugeInteger.h"
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{

    cout << "Please enter an operand, an operation, and another "
         << "operand up to 40 digits long." << endl
         << "Example: 097823049857902945 + 092837402983423";

    return 0;

}
« Last Edit: February 13, 2013, 03:09:15 pm by Dreyth »
I'm LAKERS from The Vertical Summit

seifullaah73

  • Hero Member
  • *****
  • Posts: 3095
  • Respect: +1190
    • View Profile
    • Email
Re: Help with compiling this stupid C++ thing
« Reply #7 on: February 13, 2013, 05:29:05 pm »
0
I hope i can be of some help.

The error

undefined reference to '' means the compiler has heard of it, but doesn't know exactly how to use it, which means that you have not linked to the library code BigInteger.

Do you use a makefile to compile the code.

maybe something like this would help.

Quote
You need to use both -L and -l switches. -L to point to the directory containing the library binary, -l to name that binary

e.g. -L/home/ed/libs -lmath

Lets see what entropy has to say about this, i may be wrong.
errors just p**s me off.
----------------------------------------------------------------
So, verily, with every difficulty, there is relief.
Holy Quran 94:5
-------------------------------------------------------------
Measuring reminder:
5 toe to heel steps = 148cm
------------------------------------------------------------------------

�Strength comes from the legs, Power comes from the torso and Speed comes from the arm.� � Al Vermeil

My Progress Log
A Journey to Running fast and Jumping High