2016-04-21 20 views
0

它似乎likt的公共結構是不上課標識.....下面是我的代碼:公共結構沒有在類中標識?

#include <iostream> 
#include <cstring> 


class Human { 
public: 
    int ID = 32; 
    Book humanBook; 
    void printHumanId() { 
     std::cout << "ID IS : " << ID << std::endl; 
    } 
}; 


struct DATE { 
    int year; 
    int month; 
    int date; 
}; 
struct Book { 
    char name[50]; 
    char author[50]; 
    int id; 
    DATE date; 
}; 

void printBookInfo(Book book) { 
    std::cout << "Book Author: " << book.author << std::endl; 
    std::cout << "Book Name: " << book.name << std::endl; 
    std::cout << "Date : " << book.date.month << "/" << book.date.date << "/" << book.date.year << std::endl; 
} 

int main() { 
    Book book1; 
    DATE date1; 
    Human Etaferahu; 

    std::cout << "Date Of Publishing? " << std::endl; 
    std::cin >> book1.date.date; 
    std::cout << "Month Of Publishing?" << std::endl; 
    std::cin >> book1.date.month; 
    std::cout << "Year Of Publishing?" << std::endl; 
    std::cin >> book1.date.year; 


    std::cout << "Book Name ? " << std::endl; 
    std::cin >> book1.name; 

    std::cout << "Book Author ? " << std::endl; 
    std::cin >> book1.author; 

    Etaferahu.printHumanId(); 
    Etaferahu.humanBook = book1; 
    printBookInfo(Etaferahu.humanBook); 

    return 0; 
} 

,當我運行這段代碼我得到這個錯誤:

Severity Code Description Project File Line Suppression State 
Error C3646 'humanBook': unknown override specifier Struct c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 8 
Severity Code Description Project File Line Suppression State 
Error C2039 'humanBook': is not a member of 'Human' Struct c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 53 
Severity Code Description Project File Line Suppression State 
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Struct c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 8 
Severity Code Description Project File Line Suppression State 
Error C2039 'humanBook': is not a member of 'Human' Struct c:\users\amanuel\documents\visual studio 2015\projects\struct\struct\source.cpp 54 

回答

3

只需將「人」類的定義:

#include <iostream> 
#include <cstring> 




struct DATE { 
    int year; 
    int month; 
    int date; 
}; 
struct Book { 
    char name[50]; 
    char author[50]; 
    int id; 
    DATE date; 
}; 


class Human { 
public: 
    int ID = 32; 
    Book humanBook; 
    void printHumanId() { 
     std::cout << "ID IS : " << ID << std::endl; 
    } 
}; 



void printBookInfo(Book book) { 
    std::cout << "Book Author: " << book.author << std::endl; 
    std::cout << "Book Name: " << book.name << std::endl; 
    std::cout << "Date : " << book.date.month << "/" << book.date.date << "/" << book.date.year << std::endl; 
} 

int main() { 
    Book book1; 
    DATE date1; 
    Human Etaferahu; 

    std::cout << "Date Of Publishing? " << std::endl; 
    std::cin >> book1.date.date; 
    std::cout << "Month Of Publishing?" << std::endl; 
    std::cin >> book1.date.month; 
    std::cout << "Year Of Publishing?" << std::endl; 
    std::cin >> book1.date.year; 


    std::cout << "Book Name ? " << std::endl; 
    std::cin >> book1.name; 

    std::cout << "Book Author ? " << std::endl; 
    std::cin >> book1.author; 

    Etaferahu.printHumanId(); 
    Etaferahu.humanBook = book1; 
    printBookInfo(Etaferahu.humanBook); 

    return 0; 
} 
1
class Human { 
public: 
    int ID = 32; 
    Book humanBook; 

C++編譯器從頭到尾編譯您的代碼。從文件的開始到文件的結尾。 C++編譯器不是萬能的。它會嘗試按順序編譯文件,從頭到尾。

此時,C++編譯器完全不知道「Book」是什麼。這個類的定義出現在這個文件的後面,但是現在編譯器不知道它是什麼。因此你的編譯錯誤。

+0

讓我怎麼解決?如果我把它放在底部,那麼它就不會知道人類是什麼類的,而且我不能在課堂上做類比(我認爲),就像我可以用函數做的那樣 – amanuel2

+0

你是什麼意思? 「書」課不需要知道「人」是什麼。它不使用它。我們中的一個人很困惑。 –

+0

我知道......但我只是在課堂上練習我的技能。正如我在現在的情節(https://www.youtube.com/watch?v=b8Luumpu_kE&index=25&list=PLS1QulWo1RIYSyC6w2-rDssprPrEsgtVK)...無論如何,我可以解決這個問題,或者它是一個nono? – amanuel2

相關問題