-5

I need some help with this program I am writing in class. When ever I get ready to compile it it gives me an error that there are multiple definitions and i have tried to figure out what the mistake is but i cant. your help would be be helpful and thanks in advance. It seems that the problem is with main.cpp, person.h, and person.cpp.

main.cpp

#include <iostream>
#include "person.cpp"
using namespace std;


int main(int argc, char *argv[])
{
   Person p1;
   p1.setFirstName("Carlos");
   p1.setLastName("Martinez");
   p1.setMiddleInit('X');
   p1.setStreetAddress("Avenue");
   p1.setCity("Atlanta");
   p1.setState("GA");
   p1.setZipCode("568467");
   p1.setHomePhone("829385925");
   p1.setWorkPhone("990128399");
   cout << p1.getFirstName() << endl;
   cout << p1.getLastName() << endl;
   cout << p1.getMiddleInit() << endl;
   cout << p1.getStreetAddress() << endl;
   cout << p1.getCity() << endl;
   cout << p1.getState() << endl;
   cout << p1.getZipCode()<< endl;
   cout << p1.getHomePhone() << endl;
   cout << p1.getWorkPhone() << endl;
   //system("PAUSE"); // only needed for devc++
   return 0;
}

person.h

#include "date.h"
#ifndef PERSON_H
#define PERSON_H

class Person {

    public:
    Person();
    ~Person();

    Date dateOfBirth;
    void setLastName(char*);
    void setFirstName(char*);
    void setMiddleInit(char);
    void setStreetAddress(char*);
    void setCity(char*);
    void setState(char*);
    void setZipCode(char*);
    void setHomePhone(char*);
    void setWorkPhone(char*);

    char* getLastName();
    char* getFirstName();
    char getMiddleInit();
    char* getStreetAddress();
    char* getCity();
    char* getState();
    char* getZipCode();
    char* getHomePhone();
    char* getWorkPhone();

    private:
    char lastName[21];
    char firstName[16];
    char middleInit;
    char streetAddress[26];
    char city[21];
    char state[3];
    char zipcode[11];
    char homePhone[13];
    char workPhone[13];
};
#endif

person.cpp

#include <string.h>
#include "person.h"
Person::Person(){}

Person::~Person(){}

void Person::setLastName(char *s)
{
    strcpy(lastName,s);
}

char* Person::getLastName()
{
    static char temp[21];
    strcpy(temp,lastName);
    return temp;
}

void Person::setFirstName(char *s)
{
    strcpy (firstName,s);
}

char* Person::getFirstName()
{
    static char temp[16];
    strcpy (temp,firstName);
    return temp;
}

void Person::setMiddleInit(char s)
{
middleInit = s;
}

char Person::getMiddleInit()
{
return middleInit;
}

void Person::setStreetAddress(char *s)
{
   strcpy(streetAddress,s);
}

char* Person::getStreetAddress()
{
static char temp[26];
strcpy (temp, streetAddress); // street is copied to temp which is returned
return temp;
}

void Person::setCity(char *s)
{
   strcpy(city,s);
}

char* Person::getCity()
{
static char temp[21];
strcpy (temp, city); // city is copied to temp which is returned
return temp;
}

void Person::setState(char *s)
{
   strcpy(state,s);
}

char* Person::getState()
{
static char temp[3];
strcpy (temp, state); // state is copied to temp which is returned
return temp;
}

void Person::setZipCode(char *s)
{
   strcpy(zipcode,s);
}

char* Person::getZipCode()
{
static char temp[11];
strcpy (temp, zipcode); // zipcode is copied to temp which is returned
return temp;
}

void Person::setHomePhone(char *s)
{
   strcpy(homePhone,s);
}

char* Person::getHomePhone()
{
static char temp[13];
strcpy (temp, homePhone); // homephone is copied to temp which is returned
return temp;
}

void Person::setWorkPhone(char *s)
{
   strcpy(workPhone,s);
}

char* Person::getWorkPhone()
{
static char temp[13];
strcpy (temp, workPhone); // work phone is copied to temp which is returned
return temp;
}

I also have a date.h and date.cpp but that does not seem to be the problem. date.h

#ifndef DATE_H
#define DATE_H

class Date {
public:
    Date();
    ~Date();
    void setDate( int, int, int );
    int getDay(); // method or function to retrieve a day value
    int getMonth(); // method or function to retrieve a month value
    int getYear(); // method or function to retrieve a year value
    private:
    int day; // data member of this class
    int month; // data member of this class
    int year; // data member of this class
};

#endif

date.cpp

#include <iostream>
#include <string>
#include "date.h"
using namespace std;

    Date::Date(){
    day = 1;    // this and the following lines insure that Date objects
    month = 1;  // start in a consistent state
    year = 1900;
    }

    // the following destructor releases resources used by objects of type Date
    // when they no longer exist
    Date::~Date()
    {
    }

    void Date::setDate( int d, int m, int y )
    {
    if ( y >= 1900 )
    year = y; // validates that year is 1900 or greater
    else{
    year = 1900;
    cout << "Invalid year! " << y << endl;
    }

    if ( m >= 1 && m <= 12)
    month = m;

    else{
    month = 1;
    day = 1;
    cout << "Invalid month!" << endl;}

    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
    if ( d >= 1 && d <= 31 )
    day = d;

    else   {
    day = 1;
    cout << "Invalid day! " << d << endl;
    }
    }

    else {   if ( m == 4 || m == 6 || m == 9 || m == 11 )
    {
    if ( d >= 1 && d <= 30 )
    day = d;
    else
    {
    day = 1;
    cout << "Invalid day! " << d << endl;
    }
    }
    else
    {
    if ( m = 2 )
    {
    if ( y % 4 == 0 && y % 400 != 0 )
    {
    if ( d >= 1 && d <= 29 )
    day = d;
    else
    {
    day = 1;
    cout << "Invalid day! " << d << endl;
    }
    }
    else
    {
    if ( d >= 1 && d <= 28 )
    day = d;
    else
    {
    day = 1;
    cout << "Invalid day! " << d << endl;
    }
    }
    }
    }
    }
    } // end of functionint
    Date::getDay()
    {
    int temp;
    temp = day;
    return temp;}

    int Date::getMonth()
    {
    int temp;
    temp = month;
    return temp;
    }

    int Date::getYear()
    {   int temp;
    temp = year;
    return temp;
    }

here are the errors

1 Answers1

2

In main.cpp, change "person.cpp" to "person.h", as Mike Kinghan said.

Also, in person.h, you should include the date.h file after the header guard:

#ifndef PERSON_H
#define PERSON_H

#include "date.h"
...
#endif
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
O.Y.Arpali
  • 21
  • 2