在课堂上没有发现公共结构?

时间:2016-04-21 01:14:31

标签: c++ class struct

似乎没有公共结构在课堂上被识别.....这是我的代码:

#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  

2 个答案:

答案 0 :(得分: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 :(得分:1)

class Human {
public:
    int ID = 32;
    Book humanBook;

C ++编译器从头到尾编译代码。从文件的开头到文件的结尾。 C ++编译器不是无所不能的。它尝试从头到尾按顺序编译您的文件。

此时,C ++编译器完全不知道“Book”是什么。此类的定义稍后出现在此文件中,但此时编译器不知道它是什么。因此你的编译错误。