I have a program that creates a struct. It also has a class that creates a pointer to an array of structs that is created.
I'm having trouble accessing the elements of the structs in the array, with the pointer.

Below is a sample of the code.

Struct definition in the header file.

/* Definition of the Course struct*/
typedef struct Course
{
	string cID;
	int units;
	char	grade;
};

Actual usage in the source file.

/*declare Course Array*/

Course			cArray[MAX];

/*
=======================================================================================================================
 Person Constructor 
 initializes Student ID, 
 initializes pointer to array of course records,
 and sets the current course count to 0.
 =======================================================================================================================
 */

Person::Person(int idNum)
{
	id = idNum;
	Course	*aPtr = cArray;
	cCount = 0;

}// end Student constructor

/*
 =======================================================================================================================
 Student function addCourse that adds the courses
 updates the course count,
 and auto calculates the GPA.
 =======================================================================================================================
 */
void Student::addCourse(string cou, int uni, char gra)
{
	aPtr[cCount]->cID = cou;   // add the course informaion.
	aPtr[cCount]->units = uni;
	aPtr[cCount]->grade = gra;
}

I was under the impression that i could use the -> operator.

I tried to also acces it like this:

aPtr ->cID = cou; 

aPtr ++;

Can anybody give me a hint to what I am doing wrong??

Recommended Answers

All 7 Replies

First problem is that aPtr is declared in the class constructor. It needs to be declared in the class itself so that it can be accessed by other class methods.

What is cArray? You should probably post the class itself so that we can see what you are doing.

The right syntax is:
aPtr[cCount].cID = cou; // add the course informaion.
#
aPtr[cCount].units = uni;
#
aPtr[cCount].grade = gra;

Thanks for the responses.

I have tried the suggested code above, but its crashes the program when it get to this:

aPtr[cCount].cID = cou;   // add the course informaion.
aPtr[cCount].units = uni;
aPtr[cCount].grade = gra;

I know this because I use a system("PAUSE"); before and after this section of code.


Also, to the first response, cArray is the array I created in the source file. It is an array of type Course. My struct. If you look at my first post you can see it in the source file.

Below is a copy of my header file, with the class itself.

Thanks

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Header File
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

/* Definition of the Course struct*/
typedef struct Course
{
	string	cID;
	int		units;
	char	grade;
}; 
/* Definition of the Class Person*/
class	Person
{
/*
 -----------------------------------------------------------------------------------------------------------------------
 Public member function prototypes
 -----------------------------------------------------------------------------------------------------------------------
 */
public:
	Student(int);
	void	addCourse(string, int, char);
	void	printCourse();

/*
 -----------------------------------------------------------------------------------------------------------------------
  Private member data
 -----------------------------------------------------------------------------------------------------------------------
 */
private:
	int		id;
	Course	*aPtr;
	int		cCount;
};	/* end Person Class */
commented: Thanks for using code tags. +24

First, get rid of that global cArray. In the class constructor allocate the required number of elements, such as aPtr = new Course[MAX]; . You will also have to create a class destructor to destroy that array with the delete[] operator.

First, get rid of that global cArray. In the class constructor allocate the required number of elements, such as aPtr = new Course[MAX]; . You will also have to create a class destructor to destroy that array with the delete[] operator.

I tried what you said, made the changes and it does compile, but it still crashes when it tries to get the values.

Here is my complete source.

/*
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Source code file
Class definitions
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#define MAX 45     /* set MAX to 45 */
#include "prog3.h" /* include header file*/

using namespace std;
/*declare Course Array and Quality points counter*/

int				qPoints;

/*
 ===============================================================================d========================================
 Person Constructor 
 initializes Person ID, 
 sets max courses to to 45, 
 initializes pointer to array of course records,
 initializes GPA to 0.00, 
 and sets the current course count to 0.
 =======================================================================================================================
 */

Person::Person(int idNum)
{
	id = idNum;
	mxCourses = MAX;
	gpa = 0.00;
	cCount = 0;
	Course	*aPtr;
	
	aPtr = new Course[MAX];
	
}// end Person constructor

/*
 =======================================================================================================================
 Person function addCourse that adds the courses
 updates the course count,
 and auto calculates the GPA.
 =======================================================================================================================
 */
void Person::addCourse(string cou, int uni, char gra)
{
	aPtr[cCount].cID = cou;   // add the course informaion.
	aPtr[cCount].units = uni;
	aPtr[cCount].grade = gra;

	switch(aPtr[cCount].grade)  // switch statement to add the quality points
	{
	case 'A':	qPoints += 4; break;
	case 'B':	qPoints += 3; break;
	case 'C':	qPoints += 2; break;
	case 'D':	qPoints += 1; break;
	default:	break;
	}

    //increment the course count
	cCount++;  
    //calculte the GPA                      
	gpa = (float) qPoints / (float) cCount;
}// end addCourses

/* 
 =======================================================================================================================
 Person function printCourse
 prints the array of classes for the Person
 and formats the output.
 =======================================================================================================================
 */
void Person::printCourse()
{   // print the Course ID
	for(int i = 0; i < cCount; i++)
	{
		cout << aPtr[i].cID << "  ";
	}

	cout << "\n" << "  ";
    // print the Units
	for(int j = 0; j < cCount; j++)
	{
		cout << aPtr[j].units << setw(8);
	}

	cout << "\n" << "  ";
    // print the letter grade
	for(int q = 0; q < cCount; q++)
	{
		cout << aPtr[q].grade << setw(8);
	}

	cout << endl;
	// print the GPA
	cout << showpoint << setprecision(3) << "\nGPA: " << gpa << endl;
	cout << endl;
}// end printCourses




Thanks again for the help.

delete line 40 because it is hiding the declaration in the class itself.

Ok gotcha. That makes sense.
You are awesome.
Thanks.

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.