Hi there! I'm having a homework problem, but it's with the header code I was provided in the text. When I try to use it, I get "name lookup of 'x' changed for new ANSI 'for' scoping using obsolete binding at 'x'" Did I do something wrong? Or is there an easy fix? I've been looking for hours online for an answer and all I can seem to find is that other people have had similar problems - but no answer.
Please help me!
Thanks,
David In Toledo

#ifndef _ROLE
#define _ROLE
#include <iostream>
#include "student2.h"
#include "course2.h"
using namespace std;

const int maxstu=20;

class role {
public:
        role( );
        role(char * initCourseName, int initCourseId);
        void addStudent(char * studentName, int studentId);
        void setGrade(int studentId, char grade);
        char * getStudent(int studentId);
        char getGrade(int studentId);
        void listRole( );
private:
        course theClass;
        int numStudents;
        student courseRole[20];
        char courseGrade[20];
};

role::role( ) {
        theClass = course("-", -1);
        numStudents = 0;
}

role::role(char * initCourseName, int initCourseId) {
        theClass = course(initCourseName, initCourseId);
        numStudents = 0;
}
        
void role::addStudent(char * studentName, int studentId) {
        if (numStudents < maxstu)
        {
                courseRole[numStudents] = student(studentName, studentId);
                courseGrade[numStudents] = '-';
                numStudents++;
        }
}

void role::setGrade(int studentId, char grade) {
        for (int x=0; x < numStudents; x++)
                if (courseRole[x].getId() == studentId) break;
        if (x < numStudents) 
                courseGrade[x] = grade;
}

char * role::getStudent(int studentId) {
        for (int x=0; x < numStudents; x++)
                if (courseRole[x].getId() == studentId) break;
        if (x < numStudents) 
                return courseRole[x].getName();
        else
                return "-";
}

char role::getGrade(int studentId) {
        for (int x=0; x < numStudents; x++)
                if (courseRole[x].getId() == studentId) break;
        if (x < numStudents) 
                return courseGrade[x];
        else
                return '-';
}

void role::listRole( ) {
        cout << endl << endl
             << "Class Name: " << theClass.getCourseName() << endl 
             << "Class Id: " << theClass.getCourseId() << endl 
             << endl << endl 
                 << "ID     Name              Grade" << endl
                 << "==============================" << endl;
        for (int x=0; x < numStudents; x++)
        {
                cout.width(3);
                cout << courseRole[x].getId();
                cout << "   ";
                cout << courseRole[x].getName();
                cout.width( 20-strlen(courseRole[x].getName()) );
                cout << ' ';
                cout << courseGrade[x] << endl;
        }
}

#endif

Recommended Answers

All 2 Replies

It looks to me like you will get an out of scope error here (and other places):

void role::setGrade(int studentId, char grade) {
        for (int x=0; x < numStudents; x++)
                if (courseRole[x].getId() == studentId) break;
        if (x < numStudents) 
                courseGrade[x] = grade;
}

Putting brackets in, this is equivalent to the above:

void role::setGrade(int studentId, char grade) 
{
        for (int x=0; x < numStudents; x++)   // x is in scope inside the for loop
        {
                if (courseRole[x].getId() == studentId)
                {
                      break;
                }
        }

        // x goes out of scope here.  "Binding" is now obsolete.

        if (x < numStudents)
        { 
                courseGrade[x] = grade;
        }
}

Do you want this:

if (x < numStudents)
        { 
                courseGrade[x] = grade;
        }

as part of your "for" loop? Currently the code directly above is not inside the for loop. x is meaningless outside of this loop. That is why you are getting that error. If the directly above code is part of the for loop, change it to this and it will not go out of scope:

void role::setGrade(int studentId, char grade) 
{
        for (int x=0; x < numStudents; x++)   // x is in scope inside the for loop
        {
                if (courseRole[x].getId() == studentId)
                {
                      break;
                }

                 // x is still in scope

                if (x < numStudents)
                { 
                      courseGrade[x] = grade;
                }
        }
        // x goes out of scope here.
}

Many thanks - that makes my part work like it's supposed to. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.