5

Im wanted to create a bidirectional association between 2 classes. For example class A has class B as its private attribute and class B has class A as its private attributes.

Errors which I have gotten is mainly:

Error   323 error C2653: 'Account' : is not a class or namespace name   
Error   324 error C2143: syntax error : missing ';' before '{'

(i get loads of such error)

I believe these errors got to do with how i include paymentMode.h in account.h and vice versa. I tried commenting off one inclusion in one of the classes and things work fine. May I ask how to remove such errors while I can still have my bidirectional association between account and paymentMode class?

Thank you!

Attached are the codes that I have written.

    //paymentMode.h

    #pragma once
    #ifndef _PAYMENTMODE_H
    #define _PAYMENTMODE_H

    #include <string>
    #include <iostream>
    #include <vector>
    #include "item.h"
    #include "account.h"

    using namespace std;

    class PaymentMode
    {
    private:
        string paymentModeName;
        double paymentModeThreshold;
        double paymentModeBalance; //how much has the user spent using this paymentMode;
        vector<Account*> payModeAcctList;

    public:
        PaymentMode(string);
        void pushItem(Item*);

        void addAcct(Account*);

        string getPaymentModeName();
        void setPaymentModeName(string);

        void setPaymentModeThreshold(double);
        double getPaymentModeThreshold();

        void setPaymentModeBal(double);
        double getPaymentModeBal();
        void updatePayModeBal(double);

        int findAccount(string);
        void deleteAccount(string);

    };

    #endif



              //account.h

#pragma once
#ifndef _ACCOUNT_H
#define _ACCOUNT_H

#include <string>
#include <iostream>
#include <vector>
#include "paymentMode.h"

using namespace std;

class Account
{
private:
    string accountName;
    //vector<PaymentMode*> acctPayModeList;
    double accountThreshold;
    double accountBalance; //how much has the user spent using this account.

public:
    Account(string);

    //void addPayMode(PaymentMode*);
    //int findPayMode(PaymentMode*);

    string getAccountName();
    void setAccountName(string);

    void setAccountThreshold(double);
    double getAccountThreshold();

    void setAccountBal(double);
    double getAccountBal();
    void updateAcctBal(double);

};

#endif
lucasg
  • 10,734
  • 4
  • 35
  • 57
user2114036
  • 153
  • 3
  • 13
  • 1
    [This Q&A](http://stackoverflow.com/questions/14909997/why-arent-my-include-guards-preventing-recursive-inclusion-and-multiple-symbol/14909999#14909999) explains in detail what's going on (first question/answer). – Andy Prowl Mar 10 '13 at 15:39

1 Answers1

9

You have a circular include dependency, but in this case, since class A only holds a container of pointers of class B, and vice versa, you can use forward declarations, and put the includes in the implementation files.

So, instead of

 #include "account.h"

use

class Account;

Unrelated: do not put using namespace std in header files, and if possible, nowhere. See here for more on that issue.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    problem solved :) never knew i can write something like class Account; in header file. Thanks! learned something new today. By the way, by writing "class Account" in the header file of paymentMode, is this called forward declaration? – user2114036 Mar 10 '13 at 15:52
  • 1
    @user2114036, remember that the preprocessing model in C++ is that the `#include`d file is just copied at the place where the `#include` appears. In a header file you can write any valid C++ you want. It is even possible to e.g. start a statement in the `#include`d file and end it outside. **Very** bad taste, but legal (current compilers will probably warn, it used to be a popular source of mystifing compilation failures...). – vonbrand Mar 10 '13 at 17:30
  • @user2114036 Yes, that is a forward declaration. – juanchopanza Mar 10 '13 at 17:32