对象未在作用域(std::map)中声明

Object was not declared in scope (std::map)

本文关键字:map 声明 std 作用域 对象      更新时间:2023-10-16

我有一个类,其中有一个名为students的映射,我想在其中存储另一个类的实例。我得到以下错误消息:

' Student '未在此范围内声明

std::map<int, Student> students;

下面是我代码的相关部分:

APP.H

#include <iostream>
#include <string>
#include <map>
#include "student.h"
class App
{
public:
    void AddStudent();
protected:
    std::string SName;
    std::string SEmail;
    std::map<int, Student> students;
};

APP.CPP

#include <string>
#include <iostream>
#include "app.h"
#include "student.h"
void App::AddStudent()
{
    std::cin >> SName;
    std::cin >> SEmail;
    Student Stu(SName, SEmail);
    students.insert(std::make_pair(someID, Stu));
}

STUDENT.H

#include <string>
#include <iostream>
#include "app.h"
#include "test.h"
class Student
{
public:
    Student() { }
    Student(std::string studentName, std::string studentEmail);
    int studentId;
    std::string GetStudentName() const;
    std::string GetStudentEmail() const;
    void SetStudentName(std::string sn);
    void SetStudentEmail(std::string se);
private:
    static int tempId;
    std::string studentName;
    std::string studentEmail;
};

如果我复制我的map到main,我没有收到这个错误,但是当我试图插入实例时,我无法访问它。

student.h包括app.h,但app.h包括student.h

在您的情况下,您可以从student.h中删除#include "app.h",因为它是不需要的。

把我的评论作为回答:

做以下两件事:

  1. 不需要在app.cpp中包含student.h,它已经通过包含app.h获得了

  2. student.h中不包含app.h