Home > Back-end >  why can't I declare the size for the array inside a class?
why can't I declare the size for the array inside a class?

Time:05-02

So I am working on a project and it compiles with no errors but I can't run when I declare the size of the array inside the class but when I declare it inside the class it works. This is what I get when I declare it inside the class.

c:\Users\david\OneDrive\Desktop\account>cd "c:\Users\david\OneDrive\Desktop\account" && g account.cpp -o account && "c:\Users\david\OneDrive\Desktop\account"account C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\david\AppData\Local\Temp\ccakK21Q.o:account.cpp:(.rdata$.refptr._ZN7Account1nE[.refptr._ZN7Account1nE] 0x0): undefined reference to `Account::n' collect2.exe: error: ld returned 1 exit status

I am still a newbie when it comes to coding so I have no idea what exactly this means. But this is the code can someone please help. Thank you very much.

//const unsigned n{5}; THIS IS OUT SIDE THE CLASS

class Account {

    const string accNum;
    const string name;
    int balance{0'00};
    static const unsigned n{5}; // THIS IS INSIDE THE CLASS
    int count = 0;
    unsigned e{0};
    unsigned z{0};
    unsigned tranNo{1};
    std::array<int,n> buffer;

public:

    Account(string n, string a) : name{n}, accNum{a} {}
    int deposite(int x){
        return balance  = x;
    }
    int  withdraw (int x){
        return balance  = x;
    }
    const int getBal() const{
        //out << "BALANCE: " << balance << endl;
        return balance;
    }
    string getAccNUm() const{
        //out << "ACCOUNT NUMBER: " << accNum << endl;
        return accNum;
    }
    string getName() const{
        //out << "NAME: " << name << endl;
        return name; 
    }
    string getTime() const{
        const auto opened = chrono::system_clock::to_time_t(chrono::system_clock::now()); // FOR EXTRA CREDI ASSAIGNMENT #1
        //cout << ctime(&opened) << endl;
        string time = ctime(&opened);
        return time;
    }
    /*  
   const void GetDetails();{
        getName();
        getAccNUm();
        getBal();
        cout << "ACCOUNT OPENED: ";
        getTime();
    }
*/
    void Append(int x){
        buffer [e] = x;
        e  ;
        e %= n;
          z;
    }

    void print(){
        const auto opened = chrono::system_clock::to_time_t(chrono::system_clock::now()); // FOR EXTRA CREDI ASSAIGNMENT #2
        unsigned length{min (z,n)};
        unsigned i{z <= n ? 0: (z-length) % n};
        for (unsigned j{0}; j<length; j  ){
            cout << "Transtion no." << tranNo << " was = "<< buffer.at(i  %n) << " at ";
            cout << ctime(&opened) << endl;
            tranNo  ;
        }
    }
    int converter (string m){
        int a = stoi(m);
        return a;
    }
};

void simulation(){
    string name;
    getline(cin,name);
    string AccountNum;
    getline(cin,AccountNum);

    Account x(name, AccountNum);

    assert(x.getName() == name); 
    assert(x.getAccNUm() == AccountNum);

    string money; 
    while(getline(cin,money)){
        int mon = x.converter(money);
        if (mon > 0){
            x.deposite(mon);
            x.Append(mon);
        }
        else {
            x.withdraw(mon);
            x.Append(mon);
        }
    }
    
    cout << "Name: " << x.getName() << endl;
    cout << "Account Number: " << x.getAccNUm() << endl;
    cout << "Balance: " << x.getBal() << endl;
    cout << "ACCOUNT OPENED: " << x.getTime() << endl;
    
    x.print();
}

int main(){
    simulation();
}

CodePudding user response:

Non-inline static member variables must be defined outside of the class in exactly one translation unit. Like this:

const unsigned Account::n{5};

However, this will make it a non-constant expression prior to this initialisation, so you wouldn't be able to use it as the size of the array. A solution is to use an inline variable instead:

class Account {
    static inline const unsigned n{5};

Alternatively, you can declare it constexpr which makes it implicitly inline:

class Account {
    static constexpr unsigned n{5};
  •  Tags:  
  • c
  • Related