对象没有声明,无法在课堂上显示

时间:2014-01-10 20:55:10

标签: c++

我制作了一个简单的程序,试图理解构图。我多次查看该程序,但我不明白为什么我的" 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'

1 个答案:

答案 0 :(得分:2)

您定义了变量birthObj,但使用了birthobj。请注意,在C ++中,标识符区分大小写。只需将代码更改为

即可
People brandanBalasingham("Brandan", birthObj);
//                                        ^

它会起作用。当然,printInfoprintinfo相同。