对象未在作用域(std::map)中声明
Object was not declared in scope (std::map)
我有一个类,其中有一个名为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"
,因为它是不需要的。
把我的评论作为回答:
做以下两件事:
-
不需要在
app.cpp
中包含student.h
,它已经通过包含app.h
获得了 -
student.h
中不包含app.h
相关文章:
- .cpp和.h文件中的模板专用化声明
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 在变量名后声明带有 () 的非内部类型与不使用变量名的行为不同。即 std::map<int,char>x(); - 这是怎么回事?
- 如何在声明时将对象插入 std::map
- 带有 std::map 的模板函数给出错误:变量或字段声明为 void
- C 迭代器从Typedef std :: Map作为模板参数声明
- 如何在结构中声明时在 Map 中插入值
- 函数退出时是否需要清除函数内部本地声明'map' STL?
- 如何声明将使用 map.size() 计算的 int 类型
- std::map-C++要求所有声明都有一个类型说明符
- 如何声明一个 std::map,其中枚举作为键,将具有不同签名的函数作为值
- 为什么std::map find()没有声明为noexcept
- 对象未在作用域(std::map)中声明
- map声明——错误的模板
- 无法在 std::map 成员变量中分配具有前向声明值的类
- 声明 std::map 迭代器会导致一个奇怪的错误
- 有没有一种方法可以在不声明值类型的情况下拥有std::map ?