0

I have two classes that use each other as members.. Class one:

#ifndef PROPERTY_H

#define PROPERTY_H
#include "individualProperty.h"
#include "structurizer.h"
#include "p_owner.h"
#include <windows.h>
class p_owner;
class p_property {

private:
    p_owner _Owner;
    string ownerName;
    string propertyAddress;
    string taxID;
    string postalCode;
    bool owes_taxes;
    double propertyTaxVal;
    double solidWasteTaxVal;
public:
    p_owner getOwner() { 
        return _Owner;
    }
    void setOwner(p_owner a) { _Owner = a; }

    string getPropertyAddress() { return propertyAddress; }
    void setPropertyAddress(string a) { propertyAddress = a; }

    void setTaxID(string a) { taxID = a; }
    string getTaxID() { return taxID; }

    void setPostalCode(string a) { postalCode = a; }
    string getPostalCode() { return postalCode; }

    void setTaxes(bool a) { owes_taxes = a; }
    bool getTaxes() { return owes_taxes; }

    void setPropertyTaxVal(double a) { propertyTaxVal = a; }
    double getPropertyTaxVal() { return propertyTaxVal; }

    void setSolidWasteTaxVal(double a) { solidWasteTaxVal = a; }
    double getSolidWasteTaxVal() { return solidWasteTaxVal; }

    p_property(string _taxID)
    {
        taxID = _taxID;
    }





};

#endif

And Class two:

#ifndef OWNER_H
#define OWNER_H
#include "individualProperty.h"
//#include <vector>
#include "property.h"
class p_property;
class p_owner
{

private:
    string ownerName;
    string mailingAddress;
    string mailingState;
    vector<p_property> ownedProperties;
    int numProperties;


public:

    string getOwnerName() { return ownerName; }
    void setOwnerName(string a) { ownerName = a; }
    string getMailingAddress() { return mailingAddress; }
    void setMailingAddress(string a) { mailingAddress = a; }
    string getMailingState() { return mailingState; }
    void setMailingState(string a) { mailingState = a; }

    p_property getPropertyAtIndex(int a) { 
        return ownedProperties.at(a);
    }
    void addProperty(p_property a) {
        ownedProperties.push_back(a);
        numProperties++;
    }
    int getNumProperties() { return numProperties; }


    p_owner(string _name, string _addy, string _state)
    {
        setOwnerName(_name);
        setMailingAddress(_addy);
        setMailingState(_state);
        numProperties = 0;
    }

    p_owner()
    {
        setOwnerName("null");
        numProperties = 0;
    }




};

#endif

Upon building I get the error: Error Image

Which is odd because this solution built perfectly fine yesterday! Does anyone have any insight on the source of this issue?

  • 1
    Possible duplicate of [Resolve header include circular dependencies in C++](http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies-in-c) – drescherjm Jun 09 '16 at 16:39

2 Answers2

0
class p_owner;
class p_property {
    p_owner _Owner;
    ...
}

This won't work because p_owner is not defined yet. What you can do is declare a p_owner pointer instead. For example:

class p_owner;
class p_property {
    p_owner *ptr_owner;
    ...
}

Then use pointers for your get/set problem

p_owner* getOwner() { return ptr_owner; }
void setOwner(p_owner* a) { ptr_owner = a; }
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
0

Don't include property.h in owner.h. Only forward declare p_property (as you already do). Now move all the method bodies that refer to p_property or its members from owner's header to owner's cpp file. That'll break the dependencies.

I think you also have to add constructor/destructor decls to the decl of p_owner and provide bodies (even if empty) in owner.cpp as well.

The goal is to not force your field of vector<p_property> to instantiate any std::vector<p_property> methods until you're compiling the body of p_owner. The compiler generated constructor/destructor will cause the vector to be instantiated by the header.

It's not clear this is really what you want! By having a p_property directly contain a p_owner, and a p_owner contain a vector of directly contained p_propertys - you're not getting the graph structure you probably want! Most likely you want p_property to have a pointer to a p_owner. Possibly you also want p_owner to have a vector of pointers to p_propertys. But the previous paragraphs answer the question you asked, only you can determine if it is the right question.

davidbak
  • 5,775
  • 3
  • 34
  • 50
  • Thank you both! I just ended up removing the mutual class dependency and passing the data I needed as another data type. But Glad to see I wasn't too far off – Nnamdi Nwajagu Jun 14 '16 at 16:29