objects in classes

Hi guys can someone clear this up for me,I made a Birthday object inside the People class I thought this would work as I included the Birthday.h file in the People.cpp file and people.h but I get an error,the error is

line 8 error:field 'bo' has incomplete type 'people
line 8 error: definition of implicitly-declared 'people::people()'

here is my code

people.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
#ifndef PEOPLE_H
#define PEOPLE_H
#include "birthday.h"

class people
{
    public:
        people(bo);

    protected:

    private:
        birthday birthObject;
};

#endif // PEOPLE_H 



people.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include "people.h"
#include <iostream>
#include "birthday.h"

using namespace std;


people::people()
: birthObject(bo)
{

}



birthday.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef BIRTHDAY_H
#define BIRTHDAY_H


class birthday
{
    public:
        birthday(int d,int m,int y);


    private:
};

#endif // BIRTHDAY_H



birthday.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13


#include "birthday.h"
#include <iostream>

using namespace std;


birthday::birthday(int d,int m,int y)
{
    //ctor
}

closed account (E0p9LyTq)
Your people class constructor declaration and definition do not match, nor do you specify the parameter type in the contructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PEOPLE_H
#define PEOPLE_H

#include "birthday.h"

class people
{
public:
   people(birthday bo);

protected:

private:
   birthday birthObject;
};

#endif 


1
2
3
4
5
6
7
8
#include "birthday.h"
#include "people.h"

people::people(birthday bo)
   : birthObject(bo)
{
   // ctor
}
Last edited on
I don't understand how they don't match I'm looking blank at them and they look the same in both files and i don't know what doesn't match

but I also want it to be of type Birthday I'm so confused
closed account (E0p9LyTq)
Your constructor:

people(bo);

You are not specifying a data type for your constructor parameter. The constructor needs to be:

people(birthday bo);]
yeah your right sorry,I was looking at the updated code,

the thing is I'm still getting an error even though I've changed everything

the errors are

line 8 error: expected ')' before 'bo'
line 11 error: 'birthday' does not name a type


I can't see any errors in that code i don't know why I'm getting them error messages =(
anyone have any idea as what I can do to fix the errors,

Thanks
closed account (E0p9LyTq)
Change your constructor, as I pointed out yesterday, if you haven't done it already. You have to specify a data type as part of the parameter list.

Reposting your updated code would help as well.
Last edited on
I changed my constructor but still not too sure whats wrong with the code,I'll post the updated code,here it is


birthday.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include "birthday.h"
#include <iostream>
#include "people.h"


using namespace std;


birthday::birthday(int d,int m,int y)
{
     day = d;
     month = m;
     year = y;
}

void birthday:: printDate(){


}



bithday.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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

class birthday
{
    public:
        birthday(int d,int m,int y);
        void printDate();

    private:
        int day;
        int month;
        int year;

};

#endif // BIRTHDAY_H




people.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include "people.h"
#include <iostream>
#include "birthday.h"

using namespace std;


people::people(birthday bo)
: birthObject(bo)
{

}

void people:: printInfo(){


}



people.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#ifndef PEOPLE_H
#define PEOPLE_H
#include "birthday.h"

class people
{
    public:
        people(birthday bo);
        void printInfo();
    private:
        birthday birthObject;
};

#endif // PEOPLE_H



the errors are

line 8 error: expected ')' before 'bo'
line 11 error: 'birthday' does not name a type
I have a theory, but I'm not at a computer that has compiler so I'm not able to test the theory.

The constructor for birthday class requires 3 integers as parameters, so for the constructor definition in people.cpp, wouldn't you have to provide 3 integer parameters for birthObject object like shown below? I'm guessing you are trying to copy the variables of bo object to the variables of birthObject object. Am I right?

1
2
3
4
5
people::people ( birthday bo ):
birthObject ( bo.day , bo.month , bo.year )
{

}

But this wouldn't work because day, month and year are all private members of the birthday class. You would need get functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#include "people.h"

class birthday
{
    public:
        birthday(int d,int m,int y);
        void printDate();

        int getDay ();
        int getMonth ();
        int getYear ();

    private:
        int day;
        int month;
        int year;

};

#endif // BIRTHDAY_H 

1
2
3
4
5
people::people (birthday bo):
birthObject ( bo.getDay () , bo.getMonth () , bo.getYear () )
{

}


Like I said, I don't have a compiler at the moment to test this idea, so if you can test it for me, I would appreciate it. I apologize in advance if this is incorrect or if I made a wrong assumption about what you are trying to accomplish.
Last edited on
Here are a few online compilers that might interest you in the future:
http://ideone.com/
http://coliru.stacked-crooked.com/ <- this one supports multiple files in a roundabout way
http://cpp.sh/

Just looking at your code snippet, it looks like it should work. I would recommend a copy constructor for your birthday class though.

Edit: Uh oh, you have a circular dependency present.
birthday.h includes people.h
people.h includes birthday.h
The solution to this problem is to either redesign the data interactions or use a technique called forward declaration.
However, in your case you do not actually need to include people.h in birthday.h, so just remove that include.
Last edited on
wow kevin that actually worked,thanks

how come Including people.h in birthday.h gave me an error I thought It would have no effect on the program
Last edited on
I will let someone else explain circular dependencies since they do it better:
http://stackoverflow.com/a/3127374/2607949

Remember to mark the thread as solved, if your question is answered :)
thanks,I'll try figure this out
Topic archived. No new replies allowed.