-2

I want to create a object array of Accounts so I can manage them load everything from a file (by struct). Im pretty new leaning c++ but I have no idea what I am doing wrong.

What does: Account** accounts[50] ? "" accounts[i] = new Account*; "" accounts[i]->newAccount(i, id_string, pw_string, level_int); ERROR MESSAGE: request for member 'newAccount' in '* accounts[i]', which is of non-class type 'Account*'

AccountManagerFrm.cpp // Mainfile to run everything

#include "AccountManagerFrm.h"
#include "Account.h"
#include "ladeAccounts.h"
using namespace std;
Account** accounts [50];
void AccountManagerFrm::createAccountClick(wxCommandEvent& event)
{    

    accounts[i] = new Account*;
    accounts[i]->newAccount(i, id_string, pw_string, level_int);  // ERROR LINE    

}

Account.cpp

class Account
{
    struct iAccount
    {
        string ID;
        string password;
        int level;
    };
Account()
    {

    } 
void newAccount(int anzahl, string username, string pw, int lvl)
    {
        iAccount neu;
        neu.ID = username;
        neu.password = pw;
        neu.level = lvl;

    }


};

Account.h

#include <string>
using namespace std;
class Account{

public: 
    Account();    
    void newAccount(int anzahl, string username, string pw, int lvl);   
    void getInformationFromFile();


};
spyce
  • 11
  • 2

1 Answers1

2

I want to create a object array of Accounts

That's just

Account accounts[50];

not your weird array of pointers to pointers. Then you can access one with .

accounts[i].newAccount(i, id_string, pw_string, level_int);

You'll also need to fix up the class definition. The definition itself, in the header, needs to contain all members. Also, the header should have a guard, to avoid errors if you include the header more than once. It's a bad idea to dump namespace std; into the global namespace; this pollutes the global namespace for everyone who includes the header. The whole header should be something like

#ifndef ACCOUNT_H
#define ACCOUNT_H

#include <string>

class Account {
public: 
    Account();    
    void newAccount(int anzahl, std::string username, string std::pw, int lvl);   
    void getInformationFromFile();
private:
    std::string ID;
    std::string password;
    int level;
};
#endif

The source file should just define the member functions, not redefine the whole class:

#include "Account.h"

Account::Account() {}

void Account::newAccount(int anzahl, std::string username, std::string pw, int lvl)
{
    ID = username;
    password = pw;
    level = lvl;
}

If you're struggling with basic class definitions, then you really should read a good introductory book. This is a complicated language, and you'll never learn it by guessing the syntax.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I already tryed this but then I get the Error: [Linker Error] undefined reference to `Account::newAccount(int, std::string, std::string, int)' If I try to use a constructor "Account(int, ... ... ...) I get ERROR: invalid use of 'Account::Account' – spyce Oct 27 '14 at 13:09
  • 1
    @spyce That's because you defined two `Account` classes, one in the header and one in the `cpp`. The member functions in the "header-Account" have no definition. You should review the basics of how to define classes and their members in your fine book. – molbdnilo Oct 27 '14 at 13:17
  • @Mike Seymour Can u give me an example of a Headerfile + the .cpp File used in a main-class? I dont have a book right now changed alot but still got an error: invalid use of 'Account::Account' – spyce Oct 27 '14 at 13:35
  • @spyce: I've added examples that should work (although I haven't tested them). But you really will need a book if you want to learn C++. – Mike Seymour Oct 27 '14 at 13:53