【问题标题】:Public struct not identified in class?类中未识别公共结构?
【发布时间】:2016-04-21 01:14:30
【问题描述】:

似乎没有在类中识别公共结构.....这是我的代码:

#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  

【问题讨论】:

    标签: c++ class struct


    【解决方案1】:

    只需移动'Human'类的定义:

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

    【讨论】:

      【解决方案2】:
      class Human {
      public:
          int ID = 32;
          Book humanBook;
      

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

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

      【讨论】:

      • 那么我该如何解决?如果我把它放在底部,那么它就不会知道人类是什么类..我不能在类中做多态(我认为),就像我可以用函数做的那样
      • 什么意思? “书”类不需要知道“人”是什么。它不使用它。我们中的一个人很困惑。
      • 我知道...但我只是在上课练习我的技能。正如我现在的那一集(youtube.com/…)......无论如何我可以解决这个问题或者它是一个nono?
      • 我认为绝对没有错,无论如何,您希望“解决这个问题”。做到这一点。
      • @AmanuelBogale 你显然不把它放在底部,然后 int main() 会不高兴。必须放在使用的地方之前,但是在struct Book的声明之后,因为在编译开始的时候,程序在定义之前根本不知道Book是什么
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      相关资源
      最近更新 更多