I'm getting the error "no 'void Birthday::printDate()' member function declared in class 'Birthday'" When trying to execute my code, I'm learning from videos/books and have copied the code exactly but it won't work..? Any ideas
I normally google around for 20 minutes and sort issues, but this time I'm stuck.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include "Birthday.h"
#include "People.h"
usingnamespace std;
int main()
{
Birthday birthObj (12.28.1986)
People BJB("BJB",birthObj);
BJB.printInfo()
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
class Birthday
{
public:
Birthday(int m, int d, int y);
void PrintDate(){}
private:
int month;
int day;
int year;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "Birthday.h"
#include "People.h"
#include <iostream>
usingnamespace std;
Birthday::Birthday(int m, int d, int y)
{
day = d;
month = m;
year = y;
}
void Birthday::printDate(){ <<<<Error comes in here>>>>
cout << day << "/" << month << "/" << year << endl;
}
In Birthday.hpp when you declare your function with void PrintDate(){} you are telling the compiler that your function does nothing by not putting anything in the two curly braces. You could put your code inside those but if you want to write your methods in other .cpp files you need to use a function prototype by using a semi colon. void PrintDate();
C++ is case sensitive. The Declaration and definition must match.
voidPrintDate() const;{}
void Birthday::printDate() const {
If you are going to put function definitions in a cpp file don't put the braces in the header file, that is defining the function twice.
Another improvement is to get rid of the usingnamespace std; everywhere, just put std:: before every std thing. Google to see why. It is also what all the experienced C++ coders do.
If a function does not change the value of a member variable, then that function can b marked const make sure that the definitions and declarations match when you do that.Functions that differ only in their const-ness are different functions.
Also, function parameters that won't be changed inside a function can also be const, and instances of classes should be passed by reference, although neither parameter is used here:
1 2 3 4 5
void People::printInfo(const string& x, const Birthday& bo) const{
std::cout << name << " was born on ";
dateofBirth.printDate()
}