| New Reply |
How do you access a pointer of a class nested in another class |
Share Thread | Thread Tools |
| Mar16-12, 04:35 PM | #1 |
|
|
How do you access a pointer of a class nested in another class
Given the following:
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;
}
|
| Mar18-12, 01:28 AM | #2 |
|
|
I'm not sure I completely understand what you're being asked to do here. You said you added the getAllGrades() function, so does that mean everything else was given to you? And the goal is to implement the addGrade() function, or something else?
If so, then is addGrade() allowed to put the new grade at the beginning of the list, or does it have to be at the end? If it's allowed to be at the beginning, then you ought to be able to just create a new pairList whose 'next' member points to allGrades, and then replace allGrades with your new pairList. You don't need a special accessor for that member, since you're accessing it from within the class. Once that's done, you should be able to implement readForwardList() by simply calling addGrade() repeatedly. |
| Mar18-12, 01:44 AM | #3 |
|
Recognitions:
|
Code:
access->getAllGrades()->getNextEntry() =
new pairList(studentID, grade, NULL); // I get an invallid lvalue in assignment here
|
| Mar18-12, 01:49 AM | #4 |
|
|
How do you access a pointer of a class nested in another class |
| Mar18-12, 03:25 AM | #5 |
|
Recognitions:
|
Getting back to the original question:
Code:
intList *intList::addList(intList * newList){
if(this == NULL){ // if current list is empty
return(newList); // return newList
}
intList *tmpList = this; // append newList to this list
while(tmpList->nextEntry != NULL)
tmpList = tmpList->nextEntry;
tmpList->nextEntry = newList;
return(this);
}
int main(){
using namespace std;
intList *theList = NULL;
int i;
for(i = 0; i < 8; i++) // initialize list {0..7}
theList = theList->addList(new intList(i, NULL));
|
| Mar18-12, 11:51 AM | #6 |
|
|
Hi, Thanks for the replies. The question is: Define a new class, pairList which is like intList except that each cell contains two integers instead of one. Its operations are the same as intList, except that getValue() is replaced with getFirstValue() and getSecondValue(), and its constructor is also changed approppriately. Define the operation pairList *search(int val), which looks for a cell that has val as its first and returns a pointer to that cell.
Using 'pairList' define the class grades that keeps a grade list, consisting of student ID numbers (integres) and grades on one exam, and reports grades in terms of z-scores. 'mean' and 'stdDev' hold the mean and standard deviation of the scores but should not be computed each time a new grade is added. Instead, compute them only when one of the functions 'average', 'standardDeviation' or 'getZScores' is called. Set 'meanValid' to true when 'mean' and 'stdDev' are valid, and to false when they may not be. What I 'm asking is, for example, if I had the following : Code:
struct addr {
char name[30];
char street[40];
char state[3];
unsigned long int zip;
}
Code:
struct emp {
struct addr address; // nested structre
float wage;
} worker;
worker.address.zip = 12345; Can I not do something similar with pairList's elements given pairList *allGrades is a pointer of member of 'grades'. I don't think I can as no instance of pairList is defined in grades only a pointer to pairList, (as well as the member allGrades being private and not public; So I don't know where to start? I had tried making both nextEntry in pairList and allGrades public, but then I got a run time segmentation fault, and that is not the question anyway. The question assumes that it can be done the way itis setup. |
| Mar18-12, 01:28 PM | #7 |
|
Recognitions:
|
Code:
grades::addGrade((int studentID, int grade){
allGrades = new pairList(studentID, grade, allGrades);
}
Code:
class grades {
// ...
grades() {allGrades = NULL; mean = 0.; stdDev = 0.; meanValue = 0;}
// ...
|
| Mar18-12, 02:30 PM | #8 |
|
|
Hi. Thanks for helping. In the grades constructor: grades() { } // start with empty lists, that is all he says. meanValue is set true when mean and stdDev are valid otherwise false. Because you are only to compute mean and stdDev each time average() , standardDeviation() and getZScore() is called They are not to be computed each time a new grade is added.
No, I will not use the struct code example because I didn't even keep the code with the segmentation fault, I was just experimenting at the time. Thanks for the offer to look at it , though. I thought the question a bit open ended too. |
| Mar18-12, 02:41 PM | #9 |
|
Recognitions:
|
|
| Mar19-12, 06:23 PM | #10 |
|
|
I replaced in the above readForwardList(): front = new pairList(inputval1, inputval2, front); with theGrade->addGrade(inputval1, inputval2); where theGrade is a pointer to grades, and where addGrade() is defined as in post #7. The program compiled fine, but when I ran it I got a segmentation fault on entering the first line of data.
|
| Mar19-12, 06:59 PM | #11 |
|
Recognitions:
|
Also shouldn't the name of readForwardList() be something like inputGrades()? |
| Mar19-12, 07:05 PM | #12 |
|
|
It is ok I got it working; I changed the initialization of the grades constructor from: grades() {allGrades = NULL; meanValue = false; } to, grades() : allGrades(NULL) { meanValue = false; }. Thanks, very much for helping all along.
|
| Mar19-12, 08:52 PM | #13 |
|
Recognitions:
|
Are you sure you didn't use grades(){allGrades == NULL; meanValue = false;} (== instead of =)? This is working for me. Also I added a destructor to delete all the allocated pariLists in allGrades:
Code:
class grades {
private:
pairList *allGrades; // list of grades
float mean, stdDev;
bool meanValue;
public:
grades(){allGrades = NULL; meanValue = false;}
~grades(); // destructor
void addGrade(int studentID, int grade);
void reverseM(){allGrades = allGrades->reverseM();}
void showGrades();
};
grades::~grades(){
pairList *tmpList;
while(allGrades != NULL){
tmpList = allGrades;
allGrades = allGrades->getNextEntry();
delete tmpList;
}
}
|
| Mar19-12, 09:01 PM | #14 |
|
|
OK, What I have done now is use a global pointer to grades (so that the average(), getZScore(), etc can access the input data) and now again, I am getting a segmentation fault. I have replaced readForwardList() with inputGrades().
Code:
grades *theSubject = NULL; // global scope
....
void inputGrades()
{
int inputval1, inputval2;
// grades theSubject; local scope
cout << "Enter studentID no: "; cin >> inputval1;
cout << "Enter grade: "; cin >> inputval2;
while (!cin.eof()) {
theSubject->addGrade(inputval1, inputval2);
cout << "Enter studentID no: "; cin >> inputval1;
cout << "Enter grade: "; cin >> inputval2;
}
cout << '\n';
}
int main()
{
//grades *theSubject; local scope
inputGrades();
// mean();
// deleteList(theSubject->getAllGrades());
return 0;
}
|
| Mar19-12, 09:06 PM | #15 |
|
|
No, I am sure of that. The book has not introduced us to destructors yet, infact another 10 pages has to go before before the book talks about delete. Thanks.
|
| Mar19-12, 09:49 PM | #16 |
|
Recognitions:
|
Code:
grades *theSubject = NULL;
int main()
{
// ...
theSubject = new grades;
// ...
theSubject->addGrade(...);
// ...
}
Code:
grades theSubject;
int main()
{
// ...
theSubject.addGrade(...);
// ...
}
Code:
pariList * allGrades(NULL); pairList * allGrades = NULL; |
| Mar20-12, 08:46 AM | #17 |
|
|
It is working fine now. The lack of an instance of grades was the problem,I guess. I thought I had allGrades initialized the way I said I had, I actually had the initialization of allGrades left out altogether. Thank you for your time and going to the trouble of typing the program into your own computer. Thanks again.
|
| New Reply |
| Thread Tools | |
Similar Threads for: How do you access a pointer of a class nested in another class
|
||||
| Thread | Forum | Replies | ||
| Waitlists to get into class at junior college math class ? | Academic Guidance | 16 | ||
| How could I call a base class virtual function from the derived class ? (C++) | Programming & Comp Sci | 2 | ||
| Why does Chern class belong to INTEGER cohomology class? | Differential Geometry | 41 | ||
| How does a Physics class differ from an Engineering class? | Academic Guidance | 38 | ||
| Access members of another class? | Programming & Comp Sci | 1 | ||