A. Horvath
A. Horvath

Reputation: 21

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

I have a class with a map called students where I would like to store instances of another class. I get the following error message:

‘Student’ was not declared in this scope

std::map<int, Student> students;

Here is the relevant part of my code:

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

If I copy my map to main, I don't receive this error, but then I can't access it when I try to insert the instance. I'd rather not have this map in main.

Upvotes: 1

Views: 1747

Answers (2)

CinCout
CinCout

Reputation: 9619

Making my comment as an answer:

Do the following two things:

  1. No need to include student.h in app.cpp, it already gets it via the inclusion of app.h

  2. Do not include app.h in student.h

Upvotes: 0

Andr&#233; Sassi
Andr&#233; Sassi

Reputation: 1086

student.h includes app.h, but app.h includes student.h.

In your case, you can just remove the #include "app.h" from student.h, because it is not needed.

Upvotes: 1

Related Questions