SeaBebop
SeaBebop

Reputation: 33

C++ Class w/ struct 'undeclared identifier"

So I am getting an error that my privates are not being declared when in use. I feel like my initialization in my cpp file might be the problem but there is nothing that I can see is wrong.

The errors I am getting are:

main.cpp:55:35: error: use of undeclared identifier 'MailingAddress'
    testStudent.setMailingAddress(MailingAddress);
main.cpp:56:36: error: use of undeclared identifier 'PhysicalAddress'
    testStudent.setPhysicalAddress(PhysicalAddress);

This is my Student.h file.

#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <string>
using namespace std;

struct Address
{
  string street, city, state, zip;
};
class Student{ 
  private:
    string name;
    Address MailingAddress;
    Address PhysicalAddress;
    double age;
  public:

    Student();
    Student(string name, Address MailingAddress,Address PhysicalAddress, double age);
    ~Student();

    void setName(string iname);
    void setMailingAddress(Address iMailingAddress);
    void setPhysicalAddress(Address iPhysicalAddress);
    void setAge(double iage);

    string getName();
    Address getMailingAddress();
    Address getPhysicalAddress();
    double getAge();
};

#endif

This is my .cpp file.

#include "Student.h"
#include <string>
//INITIALIZATION

Student::Student(string n,Address mA,Address pA,double a)
{
  name = n;
  MailingAddress = mA;
  PhysicalAddress = pA;
  age = a;
}
Student::~Student()
{
cout << "Instance removed from memory" << endl;
}

//SETTERS
void Student::setName(string name)
  {name = name;}

void Student::setMailingAddress(Address MailingAddress)
  {MailingAddress = MailingAddress;}

void Student::setPhysicalAddress(Address PhysicalAddress)
  {PhysicalAddress = PhysicalAddress;}

void Student::setAge(double age)
  {age = age;}
//GETTERS
string Student::getName()
  {return name;}

Address Student::getMailingAddress()
  {return MailingAddress;}

Address Student::getPhysicalAddress()
  {return PhysicalAddress;}

double Student::getAge()
  {return age;}

And my main.cpp

#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

int main() {

    double age;
    string name;
    string street0, city0, state0, zip0, street1, city1, state1, zip1;


    cout << "Enter the student's name: ";
    cin >> name;

    cout << "Enter the student's age: ";
    cin >> age;
    cout << "Enter the student's mailing address (street, city, state, zip): ";
    cin >> street0 >> city0 >> state0 >> zip0;

    cout << "Enter the student's physical address (street, city, state, zip): ";
    cin >> street1 >> city1 >> state1 >> zip1;

     // Add the proper lines of codes to create an instance of type Student. Name this instance testStudent
    Student testStudent;
    testStudent.setName(name);
    testStudent.setMailingAddress(MailingAddress);
    testStudent.setPhysicalAddress(PhysicalAddress);
    testStudent.setAge(age);
    // Note the next few lines of code will not compile until testStudent is declared and initialized

    // Printing using accessors
    cout << "Student Info:" << endl;
    cout << "\tName: " << testStudent.getName() << endl;
    cout << "\tAge: " << testStudent.getAge() << endl;
    cout << "Mailing Address: " << endl << "\t" << testStudent.getMailingAddress().state << endl
                    << "\t" << testStudent.getMailingAddress().city << endl
                    << "\t" << testStudent.getMailingAddress().state << endl
                    << "\t" << testStudent.getMailingAddress().zip << endl;

    cout << "Physical Address:" << endl << "\t" << testStudent.getPhysicalAddress().state << endl
                    << "\t" << testStudent.getPhysicalAddress().city << endl
                    << "\t" << testStudent.getPhysicalAddress().state << endl
                    << "\t" << testStudent.getPhysicalAddress().zip << endl;

    return 0;

}

Upvotes: 1

Views: 675

Answers (2)

nkvns
nkvns

Reputation: 600

Error message is quite clear here. In scope of main MailingAddress and PhysicalAddress are not available.

Couple of points about code sample

  1. Getters in Student class should be marked const
  2. In setters, Address should be taken as const &. Same for Student constructor taking MailingAddress and PhysicalAddress as parameters.

Upvotes: 0

Acorn
Acorn

Reputation: 26196

This:

testStudent.setMailingAddress(MailingAddress);
testStudent.setPhysicalAddress(PhysicalAddress);

does not make sense. MailingAddress and PhysicalAddress are your private members in testStudent of type Address.

You have to create an Address first, from the data you got from the user, and then pass it to the set* methods.

Upvotes: 1

Related Questions