0

I am currently working on a project with several other people. We originally had one header file with class and method declarations and one .cpp file with implementation/main. We decided to try and move the function implementation to the header files so we could all work on different features at the same time without producing as many merge conflicts in git.

When I compile the code, I get "undeclared identifier" errors and others that follow, leading me to the conclusion that something is wrong with the way my headers are set up. The code is long, so I'll try to provide a relevant example:

(Assignment.h and Student.h contain classes in a similar setup)

Instructor.h:

    #pragma once
    #include "Assignment.h"
    #include "Student.h"
    #include "Course.h"
    #include <string>
    #include <vector>
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <algorithm>

    using namespace std;

    class Instructor {
    private:
        std::string instructorName;
        std::vector<Course*> courseList;
    public:
        Course *currentCourse;
        Instructor();
        Instructor(std::string name);
        void setName(std::string name);
        std::string getName();          
        void firstTimeInstructor();
        void addCourse();
        void addCourse(std::string name);
        void removeCourse();
        void mainMenu();
        void init();
    };

... implementation for Instructor methods...

Course.h:

#pragma once
#include "Assignment.h"
#include "Student.h"
#include "Instructor.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class Course {
private:
    std::string courseName;
    std::vector<Student*> studentList;
    std::vector<Assignment*> assignmentList;
public:
    Course(std::string name);
    void setCourseName(std::string name);
    std::string getCourseName();
    void showCourseOptions();
    void addStudent();
    void addStudent(std::string first, std::string last);
    void addAssignment();
    void addAssignment(std::string name, double points);
    void print();
    void courseMenu();
};

...implementation for Course methods...

First error: instructor.h(17): error C2065: 'Course': undeclared identifier (Similar ones follow: Student, Assignment, etc.)

I was studying this post for a while, but I wasn't really able to find a definitive answer and I wasn't sure if it even applied to my situation.

I know some of this (such as the using namespace std or even declaring functions in the header) may be bad practice, but we're all a bit new to this so any guidance on the subject would be appreciated.

zmjackson
  • 67
  • 1
  • 5
  • 4
    This seems to be a simple circular dependency with two header file requiring each other. Do some research about *forward declarations* for the way to solve this. – Some programmer dude Apr 13 '18 at 05:44
  • 1
    Might be worth a visit. https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration. – R Sahu Apr 13 '18 at 05:46

0 Answers0