Search code examples
c++scopeclass-design

scope error message


Hey guys, just learning about composition of classes and ran into this error.

Gradebook.h

  #ifndef GRADEBOOK_h
#define GRADEBOOK_h

#include "StudentRec.h"
#include <iostream>
#include <string>

class GradeBook
{
public:
    GradeBook();

    GradeBook(string initLastName, int studGrades);

    void AddStudent(string initLastName, int studGrades);

    void ShowStudents();

    void UserInterface();

private:
    static const int numStudents=20;
    StudentRec student[numStudents];
    static int studentCounter;
    static int gradeCounter;
};

#endif 

Gradebook.cpp

     #include "Gradebook.h"
#include "StudentRec.h"
#include <iostream>
#include <string>

using namespace std;

GradeBook::GradeBook()
{}

GradeBook::GradeBook(string initLastName, int studGrades)
{}

void GradeBook::AddStudent(string initLastName, int studGrades)
{   
    gradeCounter++;   //Increments variable responsible for tracking # of grades per student
    StudentRec newStudent(initLastName, studGrades); //creates new student object
    student[studentCounter]=newStudent;  //Assigns new student object to array
    studentCounter++;  //Increments variable responsible for tracking # of students
}

void GradeBook::ShowStudents()
{
    for(int i=0;i<studentCounter; i++){  //Displays information for each student instance
        cout<<student[i].GetLastName()<<' ';
        for(int j=0; j<gradeCounter; j++)        
            cout<<student[i].GetGrades(j)<<' ';
        cout<<endl;
    }
}

void GradeBook::UserInterface()
{
    char choice=' ';
    string studLastName;
    int studGrade;

    cout<<"Welcome to GradeBook, this program stores students"
    <<" grades by last name.  To ADD a student press the 'A'"
    <<" key.  To LIST all students, press the 'L' key.  To "
    <<" QUIT, press the 'Q' key."<<endl<<endl;

    cin>>choice;

    choice=toupper(choice);

    while(choice!='Q')
    {
        if(choice='A'){
            cout<<"To add a student, please enter their last name"
            <<" followed by a space and a non-negative grade"
            <<" Ex. McClure 96";
            cin>>studLastName>>studGrade;

            AddStudent(studLastName, studGrade);
        }

        else if(choice='L'){
            cout<<"This is a list of all students in GradeBook"
            <<endl<<endl;

            ShowStudents(); //Displays all StudentRec objects
        }

        else if(choice!='Q')
            cout<<"Please enter another letter"<<endl;

        cout<<"To ADD a student press the 'A' key.  To LIST all students, press the 'L' key.  To "
        <<" QUIT, press the 'Q' key."<<endl<<endl;      
    }
}

Main.cpp

    #include <iostream>
    #include <string>
#include "StudentRec.h"
#include "Gradebook.h"

using namespace std;

int main()
{
    GradeBook gradeBook;

    UserInterface();

    return 0;

}

StudentRec.cpp

#include <iostream>
#include <string>
#include "StudentRec.h"

using namespace std;

StudentRec::StudentRec()
{
    lastName=" ";
    for(int i=0;i<numGrades; i++)
        grades[i]=0;
}

StudentRec::StudentRec(string initLastName, int studGrade)
{
    static int gradeCounter=0;
    lastName=initLastName;
    grades[gradeCounter]=studGrade;
}

string StudentRec::GetLastName()
{
    return lastName;
}

int StudentRec::GetGrades(int gradeNum)
{
    return grades[gradeNum];
}

void StudentRec::AddGrades(int studGrade)
{

    gradeCounter++;
    if(gradeCounter<=numGrades)
        grades[gradeCounter]=studGrade;
    else
        cout<<"Too many grades for this student";
}

StudentRec.h

#ifndef STUDENTREC_h
#define STUDENTREC_h

#include <iostream>
#include <string>

using namespace std;

class StudentRec
{
public:
    StudentRec();

    StudentRec(string initLastName, int studGrade);

    string GetLastName();

    int GetGrades(int gradeNum);

    void AddGrades(int studGrade);

private:
    static const int numGrades=10;
    static int gradeCounter;
    string lastName;
    int grades[numGrades];
};

#endif

In the Main.cpp file, I get an error I can't find the solution for. It reads error: "UserInterface" was not declared in this scope. I got this error while compiling in XCode I got error C3861: 'UserInterface': identifier not found

Obviously i've tried it in two IDEs, I also have the StudentRec.cpp and .h, but not sure you need them. Thanks in advance for the help


Solution

  • It appears that UserInterface() is actually a member function of GradeBook, correct?

    If so, you need to add a declaration for the member function in the GradeBook class declaration:

    class GradeBook
    {
    public:
        GradeBook();
        GradeBook(string initLastName, int studGrades);
        void AddStudent(string initLastName, int studGrades);
        void ShowStudents();
    
        void UserInterface(); // Added
    
        // ...
    private:
        // ...
    };
    

    This way, the compiler will "know" that the UserInterface() function exists as a member function. You then provided the definition in void GradeBook::UserInterface() in your .cpp file.

    Then you need to call it on a GradeBook instance, like the gradeBook variable in your main() function:

    int main()
    {
        GradeBook gradeBook;
        // This calls the member function UserInterface() on the gradeBook variable.
        gradeBook.UserInterface();
        // This calls the global UserInterface(), which doesn't exist.
        // UserInterface();
        return 0;
    }