Another beginner question...

Pages: 12
Im doing some tutorial and am stuck here. Ive looked over everything closely but can not find the problem. thanks for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//main.cppp
#include "birthday.h"
#include "people.h"
#include <iostream>
using namespace std;

int main()
{
   birthday bo(6,7,1990);
   people me("A persons name", bo);
   me.printdate();
   me.printinfo();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//birthday.h
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#include "people.h"
#include "birthday.h"
#include <iostream>
using namespace std;


class birthday
{
    public:
        birthday(int m, int d, int y);
        void printdate();
    private:
        int month;
        int day;
        int year;
};

#endif // BIRTHDAY_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//birthday.cpp
#include "birthday.h"
#include "people.h"
#include <iostream>
using namespace std;

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

void birthday::printdate(){
cout << month << "/" << day << "/" << year << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//people.h
#ifndef PEOPLE_H
#define PEOPLE_H
#include "birthday.h"
#include "people.h"
#include <iostream>
using namespace std;

class people
{
    public:
        people(string x, birthday);  //error here
        void printinfo();
    private:
        string name;
        birthday dateofbirth;  //error here

};

#endif // PEOPLE_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//people.cpp
#include "people.h"
#include "birthday.h"
#include <iostream>
using namespace std;

people::people(string x, birthday bo)
: name(x), dateofbirth(bo)
{

}

void people::printinfo(){
 cout << name << "was born on: ";
 dateofbirth.printdate();
}



closed account (48T7M4Gy)
Ive looked over everything closely but can not find the problem. thanks for any help.


That's a bit vague ...
Is your program:
1) Not compiles
    * Is other code compiles correctly, i. e. is your compiler configured properly?
    * If it is, give us error message and corresponding code part.
2) Not running
    * Are you sure that it is not a problem with automatically closing console?
    * How do you run it?
    * Is there any error messages?
3) Gives an error when run
    * Is it system error message or error reported in console?
    * Give us error message and input which leads to error.
4) Not giving correct results
    * Tell what you entered, what you expected, and what you got.
    * Are you sure that results you expected are correct?

http://www.cplusplus.com/forum/articles/40071/
Here is the codeblocks error message:

||=== Build: Debug in comp2 (compiler: GNU GCC Compiler) ===|
C:\people.h|12|error: 'birthday' has not been declared|
C:\people.h|16|error: 'birthday' does not name a type|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
birthday dateofbirth; //error here

You can't do that, because you do not have a default constructor, that seems to be the main problem.

Add this to your birthday class - Birthday(){}

closed account (48T7M4Gy)
printdate() is member of birthday and not a member of people.

delete line 11 in main() and you should be OK

1
2
3
4
5
6
7
8
9
10
11
12
13
//main.cppp
#include "birthday.h"
#include "people.h"
#include <iostream>
using namespace std;

int main()
{
   birthday bob(6,7,1990);
   people me("Bob", bob);
  // me.printdate();
   me.printinfo();
}
Last edited on
I did that, but still got the same error. i think there is something wrong with my people header but im not sure yet.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>

using namespace std;

class birthday
{
public:
	birthday(int m, int d, int y);
	void printdate();
private:
	int month;
	int day;
	int year;
};

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

void birthday::printdate() {
	cout << month << "/" << day << "/" << year << endl;
}

class people
{
public:
	people(string x, birthday);  //error here
	void printinfo();
private:
	string name;
	birthday dateofbirth;  //error here

};

people::people(string x, birthday bo)
	: name(x), dateofbirth(bo)
{

}

void people::printinfo() {
	cout << name << " was born on: ";
	dateofbirth.printdate();
}


int main()
{
	birthday bob(6, 7, 1990);
	people me("Bob", bob);
	me.printinfo();

	cout << "Finished";
}

Bobwas born on: 6/7/1990
finished 
Last edited on
Hi,



On your class people, you declare a wrong constructor, try this:

 
people(string x, birthday b);


;-)
closed account (48T7M4Gy)
The constructor is OK. It already has what you recommend Emanuel in people.cpp line 7.
Thank you agian for your help, kemort! I used your code and created a new project and it ran perfectly. I then created class files inside the project and used the same code and simply copied and pasted the sections to the appropriate files in the project. I updated the "#include" for each file and when I tried to run it i get the same errors as before.

looks like the same problem I was having before. Any idea why this would be happening?
closed account (48T7M4Gy)
Hi perp,

I'm assuming all you did was delete the line I mentioned.

( The only reason I 'crunched' it all together was to make it easy to check run what I was doing. So splitting the files up and using #includes shouldn't produce any errors provided it's done properly. )

I'm not sure of this but in addition to the small error in main:

1
2
3
4
5
//people.h
#ifndef PEOPLE_H
#define PEOPLE_H
#include "birthday.h"
#include "people.h" // this might be an error 


people only requires birthday and people looking for people just might be an issue.

Give it a go and comment it out. ?? :)
birthday.h include people.h and people.h include birthday.h. Circular include, hence the error.

Only include stuff you need. birthday do not need to know about people, so it should not include people.h
Ok I think I fixed the header #includes. main.cpp has both header .h #includes, and people.cpp has people.h #include and birthday.cpp has birthday.h #include. So the .h files do not need any #include besides this;??
1
2
3
4
#ifndef filename_H
#define filename_H
#include <string>
using namespace std;


I made these changes and im still getting the same error.

The only other thing i can think of is messed up codeblocks program setting, but i wouldnt know where to start with that.
Last edited on
closed account (48T7M4Gy)
Yeah that's right or close enough to it, I was guessing but I've just tried it out and one solution is make birthday header with #pragma once directive. (compiler error was C2011)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// birthday.h
#pragma once
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

class birthday
{
public:
	birthday(int, int, int);
	void printdate();
private:
	int month;
	int day;
	int year;
};


Problem solved. What a team!

(PS and that's why crunching as one file worked so easily.)
Last edited on
I changed the birthday.h file and this is the error now:

||=== Build: Debug in composition (compiler: GNU GCC Compiler) ===|
C:\people.h|9|error: 'birthday' has not been declared|
C:\people.h|13|error: 'birthday' does not name a type|
C:\people.cpp|5|error: 'birthday' has not been declared|
C:\people.cpp||In constructor 'people::people(std::string, int)':|
C:\people.cpp|6|error: class 'people' does not have any field named 'dateofbirth'|
C:\people.cpp||In member function 'void people::printinfo()':|
C:\people.cpp|13|error: 'dateofbirth' was not declared in this scope|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|




edit here are the real errors

I was so excited for it to be solved :(
Last edited on
closed account (48T7M4Gy)
Standby and I'll post the three five files. You program runs in VS2015 with only the couple of changes - the line before and the problem Miinipaa and I were just talking about.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// people.h

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

using std::cout;
using std::endl;
using std::string;

class people
{
public:
	people(string, birthday);
	void printinfo();
private:
	string name;
	birthday dateofbirth;
};
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//people.cpp

#include "people.h"

people::people(string x, birthday bo)
	: name(x), dateofbirth(bo)
{

}

void people::printinfo() {
	cout << name << " was born on: ";
	dateofbirth.printdate();
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// birthday.h

#pragma once
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

class birthday
{
public:
	birthday(int, int, int);
	void printdate();
private:
	int month;
	int day;
	int year;
};
Pages: 12