0

my error is that the compiler is saying address is not a member of persontype type specifier my driver is array of 6 personTypes the data from the file is being read in i created adresss of type adresstype so that the data members can have access to the datamembers of adresstype and the data gets filled in correctly how do i fix my error?

#include <iostream>
#include <fstream>
#include "personType.h"
#include <string>
using namespace std;
int main()
{
    personType members[6];
    string x, i1, i2;
    ifstream myfile;
    myfile.open("infile.txt");
    for( int i = 0; i < 6; i++)
    {
        myfile >> members[i].lastName  >> members[i].firstName >> members[i].personNum >> 
            members[i].personID >> members[i].address.streetAddressNum >>
 members[i].address.streetName >> members[i].address.streetType >> members[i].address.city >>             members[i].address.stateInitials >>  members[i].address.zipCode >> members [i].gender;
        myfile>> i1 >> i2; 
        members[i].setInterest1(i1);
        members[i].setInterest2(i2);
        members[i].printPerson();
    }    
    myfile.close();
    system("pause");    
    return 0;
#include <iostream>
#include <string>
using namespace std;
class personType
{
public:
    personType();
    string firstName;
    string lastName;
    int personNum;
    char gender;
    int personID;
    addressType address;
    void setInterest1(string interest1);//mutator
    void setInterest2(string interest2);
    void printPerson();
    string  GetInterest1() const;    // Accessor
    string  GetInterest2() const;
private:
    string SetInterest1;
    string SetInterest2;
};
class addressType {
public:
    addressType();
    string streetAddressNum, streetName, streetType, city, stateInitials;
    int zipCode;
};
rich
  • 1
  • 2
  • "address is not a member of personType" was probably not the first compiler error you got. You should always look at the first compiler error first and try to understand it, since an issue more clearly described there may also be the cause of later errors. – aschepler Sep 06 '17 at 22:33

1 Answers1

0

You need to move the definition of addressType before the definition of personType since the latter has as member of type addressType.

// Define addressType before the definition of personType

class addressType {
   public:
      addressType();
      string streetAddressNum, streetName, streetType, city, stateInitials;
      int zipCode;
};

class personType
{
   public:
      personType();
      string firstName;
      string lastName;
      int personNum;
      char gender;
      int personID;

      // Can't use addressType as the type of member variable
      // unless addressType is defined first.
      addressType address;

      void setInterest1(string interest1);//mutator
      void setInterest2(string interest2);
      void printPerson();
      string  GetInterest1() const;    // Accessor
      string  GetInterest2() const;
   private:
      string SetInterest1;
      string SetInterest2;
};

Also, don't use using namespace std; in a header file. See Why is "using namespace std" considered bad practice?.

Remove that line and then use std::string instead of just string.

R Sahu
  • 204,454
  • 14
  • 159
  • 270