- #1
John O' Meara
- 330
- 0
Given the following:
My effort to access pairList within the grades class
You cannot really create a local single linked pairList with readForwardList() and yet still be an automatic member of the outer class grades, I certainly don't think? That is, how do you do it, because the *allGrades pointer is private to the class grades. And any time you place it on the l.h.s. of an assignment you get an lvalue error, using getAllGrades().
If you can see the problem I have, help is very welcome. Thanks in advance.
Code:
class pairList {
private:
int value1, value2;
pairList *nextEntry
pairList *reverseM(pairList *prev);
public:
pairList(int v1, int v2, pairList *next) : value1(v1), value2(v2), nextEntry(next) {}
int getFirstValue() { return value1; }
int fetSecondValue() { return vlaue2; }
pairList *getNextEntry() { return nextEntry; }
pairList *reverseM();
pairList *search(int val);
};
class grades {
private:
pairList *allGrades;
float mean, stdDev;
bool meanValue;
public:
grades() {} // start with empty class
void addGrade(int studentID, int grade);
float average();
int getScore(int studenID);
int getZScore(int studentID);
pairList *getAllGrades() { return allGrades; } // I added this line
};
Code:
void grades::addGrade(int studentID, int grade)
{
grades *access = this;
if(access->getAllGrades()->getNextEntry() != NULL)
access->addGrade(studentID, grade);
else
access->getAllGrades()-getNextEntry() =
new pairList(studentID, grade, NULL); // I get an invallid lvalue in assignment here
}
pairList *readForwardList() {
int inputval1, inputval2;
pairList *front = NULL;
cout << "enter ID no: " ; cin >> inputval1;
cout << "\nEnter grade: "; cin >> inputval2;
while(!cin.eof()) {
front = new pairList(inputval1, inputval2, front);
cout << "Enter ID no: "; cin >> inputval1;
cout << "\nEnter grade: " cin >> inputval2;
}
cout '\n';
return front->reverseM();
}
Code:
int main() {
grades *theGrades, *tmpGrades;
int i, j;
theGrades->gatAllGrades() = readFowardList(); // error here! how do you do it correctly?
tmpGrades = theGrades;
while(tmpGrades->getAllGrades() != NULL) {
i = tmpGrades->getAllGrades()->getFirstValue():
j = tmpGrades->getAllGrades()->getSecondValue();
cout << i << ' ' << j << ' ';
tmpGrades->getAllGrades() = tmpGrades->getAllGrades()->getNextEntry(); // error, lvalue
}
cout '\n';
// delete here
return 0;
}