having problem with composition

Am trying to use composition but it seems am failing badly, so can someone pls point me my errors...


// HEADER FILE FOR PERSON//
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include "Birthday.h"
#include <iostream>

class person
{
private:
Birthday obj;
std:: string name;

public:
person(std::string n , Birthday b);
void printdetails();

};

#endif // PERSON_H

..HEADER FILE FOR BIRTHDAY//

#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#include "person.h"

class Birthday
{
public:
Birthday(int D, int M, int Y);
void printDate();
int Date;
int Month;
int Year;
};

#endif // BIRTHDAY_H

.CPP FILE FOR BIRTHDAY//

#include "Birthday.h"
#include "person.h"
#include <iostream>

Birthday::Birthday(int D, int M, int Y)
{
Date=D;
Month=M;
Year=Y;
}

void Birthday::printDate()
{
std::cout<<Date<<"/"<<Month<<"/"<<Year<<std::endl;
}


ERROR MESSAGES//

C:\C++ CODEBLOCK\composition_of_classes\person.h|10|error: 'Birthday' does not name a type|
C:\C++ CODEBLOCK\composition_of_classes\person.h|14|error: 'Birthday' has not been declared|
C:\C++ CODEBLOCK\composition_of_classes\person.h|14|error: 'Birthday' has not been declared|


SO PLS CAN SOMEONE HELP ME OUT//


In person.h, you are trying to #include birthday.h
In birthday.h, you are trying to #include person.h

This is cyclic dependency, and can't logically happen.

In your setup, a person has-a Birthday, so person needs to know about Birthday. This means you should #include "Birthday.h" in your person.h file.

But, a Birthday does not depend on person in any way, so remove the #include "person.h" line from birthday.h.

Did you misplace your Birthday.h header or so, cause that basically compiled for me.

I added some capitalization (imo filenames, classes, methods should be capitalized) some some quality of life and rearranging (for example, avoiding cout stuff in the classes).

main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include "Person.h"

using namespace std;

int main() 
{
    vector<Person> people
    {
        { "John Smith", {13, 5, 1999} },
        { "Jane Doe", {31, 10, 2001} },
        { "Hailey Mier", {4, 6, 2003} }
    };

    for (auto& p : people)
        cout << p.AsString() << endl;

    return 0;
}


working at https://repl.it/@icy_1/LinedClientsideUser with the files on the left

Name: John Smith
Birthday: 13.05.1999

Name: Jane Doe
Birthday: 31.10.2001

Name: Hailey Mier
Birthday: 04.06.2003

@icy1:

Your version of it does not have Birthday.h #including Person.h.
What in
1
2
3
4
5
6
7
8
9
class Birthday
{
public:
  Birthday(int D, int M, int Y);
  void printDate();
  int Date;
  int Month;
  int Year;
};

requires a person anyway?
Topic archived. No new replies allowed.