范围错误消息

时间:2011-03-12 01:42:57

标签: c++ scope class-design

嘿伙计们,只是学习了类的组成并遇到了这个错误。

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

在Main.cpp文件中,我收到一个错误,我找不到解决方案。它读 错误:未在此范围内声明“UserInterface”。在XCode中编译时出现此错误 我得到错误C3861:'UserInterface':找不到标识符

显然我已经在两个IDE中尝试过了,我也有StudentRec.cpp和.h,但不确定你是否需要它们。在此先感谢您的帮助

2 个答案:

答案 0 :(得分:4)

看来UserInterface()实际上是GradeBook的成员函数,对吗?

如果是这样,您需要在GradeBook类声明中为成员函数添加声明:

class GradeBook
{
public:
    GradeBook();
    GradeBook(string initLastName, int studGrades);
    void AddStudent(string initLastName, int studGrades);
    void ShowStudents();

    void UserInterface(); // Added

    // ...
private:
    // ...
};

这样,编译器将“知道”UserInterface()函数作为成员函数存在。然后,您在void GradeBook::UserInterface()文件的.cpp中提供了定义。

然后,您需要在GradeBook个实例上调用它,例如gradeBook函数中的main()变量:

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

答案 1 :(得分:1)

UserInterface()GradeBook的一种方法。电话可能需要:

gradeBook.UserInterface();