0

In short, I've created a bank object. Using my CreateBank() method.

 Bank* Program::CreateBank()
    {

        //Creates a new Bank object.

        Bank *bank = new Bank();

        return bank;
    }

Then, when the bank has been successfully created, I basically want to be able to pass it to any other method -- It's next location should be the PopulateBank() method which will fill it with information.

    void Program::PopulateBank(Bank* bank)
    {
        bank->populate();
    }

However, the PopulateBank() method currently cannot be compiled. I have the following error preventing me from doing so:

"A nonstatic member ference must be relative to a specific object"

Here is the code I'm using for my calls:

    void Program::TestBankClass()
    {

        //Creates new bank Object.
        //Populates the bank object.
        //lists customers that were populated with all information.

        Bank* bank = CreateBank();

        PopulateBank(bank);

        ListAllCustomers(bank);

And here's my header file:

class Program
    {

    public:

        static void AccountTester();
        static void TransactionTester();
        static void CreditAccountTester();
        static void TestCustomerClass();
        static void TestBankClass();

        void ListAllCustomers(Bank* bank);
        Bank* Program::CreateBank();
        void PopulateBank(Bank* bank);

    };

Anyone know what I'm doing wrong? or could give me some tips? would really appreciate it!

7
  • 1
    It would appear that your "populate()" method is marked "static". Remove the "static" qualifier. (Or perhaps you are missing the "static" qualifier on "PopulateBank", "TestBankClass", or one of these functions?) It is difficult to tell without a more complete copy of your code. Commented Jan 29, 2016 at 0:46
  • @MichaelAaronSafyan, You can call static member functions using an object of the class. Commented Jan 29, 2016 at 0:48
  • @chris, true, but some warning/error levels will prevent it. Commented Jan 29, 2016 at 0:50
  • This is all irrelevant, read the error message clearly. It's not complaining that a static method is being called with an object, it's complaining that a non-static method is being called without an object. Commented Jan 29, 2016 at 0:51
  • @Beta That's the only non-static method he's calling without an object reference. Commented Jan 29, 2016 at 0:53

1 Answer 1

1

Program::PopulateBank is not a static method, so you have to call it through a specific object.

Program prog;
Program.PopulateBank(bank);

or

Program *prog = new Program;
prog->PopulateBank(bank);

Or you could make the PopulateBank method static, but I don't know if that will be consistent with the logic of the class. It depends on what this method does, and whether it needs access to member variables of a specific object.

Sign up to request clarification or add additional context in comments.

6 Comments

How does calling it this way make it compilable?
Ah, this makes sense, I made everything static since I don't need more than one program object, program now compiles, thanks!
If you make everything static, that means it's essentially a singleton class, you can't have more than one Program.
Yup. But I don't really need more than one program object, right? I could have multiple banks, with multiple clients, but not more than one program, that's correct right?
@Mitch89, If it has no state, it might as well be a namespace full of functions rather than a class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.