-6

was in this for few hours now and I cannot seem to find the right way to do this. I am creating a vector that will be populated with structs but I could not make it to work. I have tried making a struct and put the struct from the object in there but received an error. Anyways this is my work, I am really new into this so I hope you guys could help me out.

main.cpp

#include <iostream>
#include "CBank.h"

int main()
{
    CBank bank;

    struct account = bank.account;

    bank.account.name = "Alpha Omega";
    bank.account.money = 15635.23;
    bank.account.pin = 3241;

    bank.add.push_back(account);
    return 0;
}

CBank.h

#ifndef CBANK_H
#define CBANK_H

#include <iostream>
#include <vector>

class CBank
{
    public:
        CBank();

    private:
        struct account { std::string name;
                         float money;
                         short pin;
                       };
        std::vector<account> add;
};

#endif // CBANK_H

CBank.cpp

#include "CBank.h"

CBank::CBank()
{
    //ctor
}
Alpha
  • 1
  • 3
  • 2
    I think you would be better off reading a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as you seem to have some basic misunderstanding on what types and values are. – Rakete1111 Jun 14 '18 at 07:47
  • 2
    Read [How to Ask](https://stackoverflow.com/help/how-to-ask). Be clear about the problem you face and post the exact error messages that you get and start with a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Vishaal Shankar Jun 14 '18 at 07:47
  • @CsuGouv Note that `account` is a type. – Rakete1111 Jun 14 '18 at 07:48
  • Hi there, sorry! I have now updated the main.cpp – Alpha Jun 14 '18 at 07:48

2 Answers2

0

Your struct account and add vector are private members of CBank. To access them you need to make them public like your class constructor is.

Liam Potter
  • 1,732
  • 8
  • 24
  • 47
  • Hi there and thank you for your response. Should it be okay to have the struct and the vector in the same class at the same time? – Alpha Jun 14 '18 at 07:54
  • It should be fine however it would be good practice to put the `account` struct in a separate header file and include that in `CBank.h`. – Liam Potter Jun 14 '18 at 07:57
0

The problem is that you define a type (account) in the class. account is a type so you should not declare it in the class :

struct account { std::string name;
                 float money;
                 short pin;
               };

and then, the class becomes :

class CBank
{
    public:
        CBank();
        account acc;
        std::vector<account> add;
};

and the main :

int main()
{
    CBank bank;

    bank.acc.name = "Alpha Omega";
    bank.acc.money = 15635.23;
    bank.acc.pin = 3241;

    bank.add.push_back(bank.acc);
    return 0;
}
CsuGouv
  • 249
  • 1
  • 9
  • 1
    "`account` is a type so you should not declare it in the class". Defining types within other types is perfectly valid. – Liam Potter Jun 14 '18 at 08:06
  • Yep, but it is easier to understand it with the declaration of the type outside of the class for a beginner. I think he should learn to spot the difference between type and instance before declaring structures in classes. Maybe I am wrong.. – CsuGouv Jun 14 '18 at 08:10
  • This is so good! Thank you so much for this. I am now studying why this and that. Thank you again, @CsuGouv – Alpha Jun 14 '18 at 08:27