0
我做了一個簡單的程序,試圖瞭解構成。我查了一遍這個程序,但是我不知道爲什麼我的「birthobj」沒有被聲明,我的「printDate()」沒有被聲明。如果您能提供一些見解,我將非常感激。對象沒有聲明,無法在類中顯示
Composition.cpp
#include <iostream> #include "Birthday.h" #include "People.h" using namespace std; int main() { Birthday birthObj(11,21,1996); People brandanBalasingham("Brandan", birthobj); brandanBalasingham.printinfo(); }
birthday.h
#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 // BIRTHDAY_H
birthday.cpp
#include "Birthday.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; }
people.h
#ifndef PEOPLE_H #define PEOPLE_H #include <string> #include "Birthday.h" using namespace std; class People { public: People(string x, Birthday bo); void printInfo(); protected: private: string name; Birthday dateOfBirth; }; #endif // PEOPLE_H
people.cpp
#include "People.h" #include "Birthday.h" #include <iostream> using namespace std; People::People(string x, Birthday bo) : name(x), dateOfBirth(bo) // name = x, dateOfBirth = bo { } void People::printInfo() { cout << name << " was born on "; dateOfBirth.printDate(); }
編譯消息:
'birthobj' undeclared (first use of this function)
'class People' has no member named 'printInfo'
它的工作表示感謝!我用printInfo做了同樣的事情! – user3183403