c++ - 错误: class name does not name a type in C++

标签 c++ class pointers compiler-errors

我知道这个问题已经被问过很多次了,但我已经尝试了几种建议,例如检查拼写、确保包含头文件、大小写等,但我仍然遇到相同的错误,并且可以不知道是什么触发了它。

当我尝试使用 g++ -c Customer.h 编译 Student.h 时,出现错误“学生”未在“学生学生;”行上命名类型对于 Login.h,我不知道为什么。任何人都可以尝试查明是什么原因造成的吗?该变量应该代表该登录 ID/帐户的学生,它应该是指向 Student 对象的指针。

同样,当我尝试编译 Login.h 时,我收到错误“Login”尚未在 Customer.h 中声明 bool addAcct(Login*) 以及错误“Login”没有类型登录* 登录次数[MAX_LOGINS]。

如有任何帮助,我们将不胜感激!

学生.h:

#ifndef STUDENT_H
#define STUDENT_H
#define MAX_LOGINS 4
#include <string>
#include "Login.h" 
using namespace std;

class Student{
    public:
        Student(int = 0, string = ""); 
        int getId();
        bool addAcct(Login*);
        void print();
    private:
        int id;
        string name;
        Login* logins[MAX_LOGINS];
        int numberOfLogins;
};
#endif

登录.h

#ifndef LOGIN_H
#define LOGIN_H
#include <string>
#include "Student.h"
using namespace std;

class Login{
    public:
        Login(int = 0, float = 0); 
        int getNumber();
        void setStudent();
        void print();
    private:
        int number;
        Student student;
};
#endif

最佳答案

这里的问题是循环依赖(如评论中指出的),问题在于处理器本质上将 #include 语句作为顺序文本插入来处理。

例如,当预处理器遇到#include "student.h"时,它会一步步像:

#ifndef STUDENT_H  // <--- header guard not defined at this point, ok to proceed
#define STUDENT_H  // <--- define header guard first thing in order to prevent recursive includes
#define MAX_LOGINS 4
#include <string>
#include "Login.h"  --->  #ifndef LOGIN_H
                          #define LOGIN_H
                          #include <string>
                          #include "Student.h"  --->  #ifndef STUDENT_H
                                                      // entire body of student.h is skipped over
                                                      // because STUDENT_H header guard is defined
                          using namespace std;  <---  // so processing continues here

                          class Login{
                          // ...
                          Student student;   // <--- error because class Student is not defined
                          };

解决方案是转发不需要完整定义的声明类型,而不是#include'ing相应的 header 。

在本例中,class Login 有一个成员 Student Student;,它需要完全定义 class Student,因此 login .h 实际上必须#include "student.h"

但是,class Student 仅携带 Login* logins[MAX_LOGINS]; 指向 Login 的指针数组,并且不需要指针类的完整定义,但只是类型的前向声明。因此,可以修改 Student.h 以转发声明 class Login,从而消除循环头依赖项并允许代码编译。

// #include "Login.h"  // <--- full header not required

class Login;           // <--- forward declaration is sufficient

关于c++ - 错误: class name does not name a type in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64085054/

相关文章:

c++ - 如何使用C++中的数组虚拟初始化方法读取文件

c++ - 为什么 std::find() 不使用我的 operator==?

c++ - 基于双向链表的双端队列实现不起作用

c++ - 使用智能指针制作多态容器的正确方法是什么?

c++ - ID_BUTTON 未使用常量表达式初始化

javascript - meteor :如何导出类并在服务器上的另一个文件中使用

ruby-on-rails - 如何检查类或模块是否已命名空间?

c++ - 在什么情况下我们需要知道一个类是否微不足道?

c - 我如何在 c 中创建一个二维数组并使用指针和函数显示它?

c++ - 从 swig 返回 double * 作为 python 列表