Thomas Ball
Thomas Ball

Reputation: 153

Error "identifier not declared in this scope" - C++

C++ beginner here. So I have several functions in which I am trying to pass an element of array of pointers (which contains structures (records)). I'm having trouble doing this and I'm pretty stuck and frustrated. I'm very new to pointers and memory so go easy on me. I keep getting errors when I try to pass the specific structure element into the function. (BTW: The functions are declared in a header file)

What can I do to fix/change this and make it work? Thank you for the help, it is very much appreciated.

The error I get:

'changeAssignmentGradeForStudent' was not declared in this scope

Code:

Structure student:

typedef struct
{
   char  firstName[50];
   char  lastName[50];
   char  studentNumber[10];
   char  NSID[10];
   float assignments[10];
   float midterm;
   float final;
} Student;


void useFunctions(int recordNum)
{
   // array of 10 student references
   Student *students[recordNum];

   // values received from the user
   for (int i = 0; i < recordNum; i++)
   {
      cout << "Student " << i + 1 << ": " << endl;
      students[i] = readStudentRecordFromConsole();
   }

   cout << "Would you like to make any changes to any student records? (N or Y)" << endl;

   cin >> answer;

   if (answer == 'N')
   {
      return;
   }
   else
   {
      cout << "Which student?" << endl;
      cin >> student;
      students[student - 1] = gradeChanges(*students[student - 1], recordNum, student);
   }
}


Student *gradeChanges(Student *s, int recordNum, int student)
{
   Student *changedStudent = s;
   int     gradeChange, aNum;

   cout << "assignment number to change?" << endl;
   cin >> aNum;
   cout << "assignment to change?" << endl;
   cin >> gradeChange;

   changeAssignmentGradeForStudent(changedStudent, aNum, gradeChange); //  where the errors are
   return changedStudent;
}

void changeAssignmentGradeForStudent(Student *s, int a, int g)
{
   if (s != NULL)
   {
      s->assignments[a] += g;
   }
}

PS: Sorry if my code is not formatted correctly. If it isn't, feel free to edit it, thank you.

Upvotes: 0

Views: 2207

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

The compiler needs to see a declaration of your functions before these are referred to. You have to put that declaration before you call a function like this:

// This is the (forward) declaration:
void changeAssignmentGradeForStudent(Student* s, int a, int g); 

Student * gradeChanges(Student* s, int recordNum, int student) {

    Student *changedStudent = s;
    int gradeChange, aNum;

       cout << "assignment number to change?" << endl;
       cin >> aNum;
       cout << "assignment to change?" << endl;
       cin >> gradeChange;

   changeAssignmentGradeForStudent(changedStudent, aNum, gradeChange); //  where the errors are


   return changedStudent;
}

// This is the definition:
void changeAssignmentGradeForStudent(Student* s, int a, int g)  {

    if (s != NULL) {

        s->assignments[a] += g;

    }

}

Upvotes: 1

Kurt Stutsman
Kurt Stutsman

Reputation: 4034

The function changeAssignmentGradeForStudent was not declared or defined before it was used, so the compiler doesn't know about it yet. You can either move the function definition up before useFunctions() or declare it at the top of the file like this:

void changeAssignmentGradeForStudent(Student*, int, int);

Upvotes: 1

Related Questions