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?
||=== 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)) ===|
#include <iostream>
#include <string>
usingnamespace 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 herevoid 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";
}
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?
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.
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;??
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.)
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)) ===|
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.
// 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;
};