2
votes

In my In-Progress BigInt class, I am having trouble with the declaration of vectors. I am getting the errors:

prog.cpp: In function 'void setBig1(std::string, int)': prog.cpp:45:3: error: 'big1' was not declared in this scope
big1.push_back(dig[x]);
^

prog.cpp: In function 'voidgetBig1(int)': prog.cpp:50:11: error: 'big1' was not declared in this scope
cout << big1[x] ;

I believe that my getters and setters involved with the vector big1 are not recognizing the decleration of the vector in the 'public:' portion of the class definition. But I cannot find a solution or a definite reason for the errors. My code is Below:

//my bigInt class
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//class, constructors
//overload operators methods
//add, subtract, multiply, divide
class bigInt{//class
    public:
        bigInt();
        ~bigInt();
        void setString(string dig);
        string getString(void);
        int getDigitLength(void);
        std::string digit;
        int digitLength;
        std::string digString;
        void setBig1(string dig, int dLength);
        std::vector<int> big1;
        std::vector<int> big2;
        void getBig1(int dLength);
};
//constructors
bigInt::bigInt(void){
    std::vector<int> big1;
}
//deconstructor
bigInt::~bigInt(){

}
//getters/setters
void bigInt::setString(string dig){
    digit= dig;
    digitLength= (digit.length());
}
string bigInt::getString(){
    return digit;
}
int bigInt::getDigitLength(){
    return digitLength;
}
void setBig1(string dig, int dLength){
    for(int x= 0; x<(dLength); x++)     
    {
        big1.push_back(dig[x]);
    }
}
void getBig1(int dLength){
    for(int x= 0; x<(dLength); x++){
        cout << big1[x] ;
    }

}

int main(){
    string digString= "1"; //string
    bigInt my_int{};
    //bigInt big1<int>;
    my_int.setString(digString); //setInternalString to equal digString
    cout << my_int.getString() << endl; //prints internal string
    my_int.setBig1(my_int.getString(), my_int.getDigitLength());//sets big1 vector = to string
    my_int.getBig1(my_int.getDigitLength()); //print big1 vector

}

I greatly appreciate any assistance.

1
Missing bigInt:: on member function definitions.hmjd
big1 is a member of BigInt. You would still need an instance of bigint to access it. you can't access it directly from outside your class.Come Raczy

1 Answers

2
votes

You forgot to specify class to which the member functions are defined. Instead of

void setBig1(string dig, int dLength){
    for(int x= 0; x<(dLength); x++)     
    {
        big1.push_back(dig[x]);
    }
}
void getBig1(int dLength){
    for(int x= 0; x<(dLength); x++){
        cout << big1[x] ;
    }

write

void bigInt::setBig1(string dig, int dLength){
    for(int x= 0; x<(dLength); x++)     
    {
        big1.push_back(dig[x]);
    }
}
void bigInt::getBig1(int dLength){
    for(int x= 0; x<(dLength); x++){
        cout << big1[x] ;
    }

Also you could declare all getters with qualifier const because they do not change an object itself of the class. For example

class bigInt
{
//...

    void getBig1(int dLength) const;
};

void bigInt::getBig1(int dLength) const
{
    for ( int i = 0; i < dLength; i++ ) cout << big1[i] ;
}

and in general case instead of type int there is better to use at least type size_t or std::vector::size_type

The vectors themselves could be declared with the access control private.