类中未标识公共结构
Public struct not identified in class?
似乎在类中没有标识公共结构。。。。。这是我的代码:
#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:usersamanueldocumentsvisual studio 2015projectsstructstructsource.cpp 8
Severity Code Description Project File Line Suppression State
Error C2039 'humanBook': is not a member of 'Human' Struct c:usersamanueldocumentsvisual studio 2015projectsstructstructsource.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:usersamanueldocumentsvisual studio 2015projectsstructstructsource.cpp 8
Severity Code Description Project File Line Suppression State
Error C2039 'humanBook': is not a member of 'Human' Struct c:usersamanueldocumentsvisual studio 2015projectsstructstructsource.cpp 54
只需移动"人类"类的定义:
#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;
C++编译器从头到尾编译代码。从文件开始到文件结束。C++编译器并非无所不能。它试图按照从开始到结束的顺序编译您的文件。
在这一点上,C++编译器完全不知道"Book"是什么。这个类的定义出现在这个文件的后面,但在这一步上,编译器不知道它是什么。因此,您的编译错误。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 有没有更好的方法来处理将标识分配给层次结构中的类以供运行时使用
- 类中未标识公共结构
- 如何使用整数 id 自动标识类层次结构中的类
- 标识应用程序的体系结构
- 结构和类的Boost融合序列类型和名称标识