简体   繁体   中英

Public struct not identified in class?

It seems likt the public struct is not identified in class..... Here is my code:

#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;
}

And When i run this code i get this error:

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  

Simply move the definition of the 'Human' class:

#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;
}
class Human {
public:
    int ID = 32;
    Book humanBook;

The C++ compiler compiles your code from start to finish. From the begining of the file, to the end of the file. The C++ compiler is not omnipotent. It attempts to compile your file in order, from start to end.

At this point, the C++ compiler has absolutely no clue what "Book" is. The definition of this class appears later in this file, but at this point the compiler has no clue what it is. Hence your compilation errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM