Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access an array of class in a function

Tags:

c++

arrays

class

I made an Array of Class st[5]. And tried to display st[5]'s data by using a function. But it doesn't work.

Only the 1st class (st[0]) is displayed, and the 'debug error Message'. I don't know what the problem is.

A main function is at the bottom.

#include <iostream>
using namespace std;

#define MAX 5       //size of array

//Class 'Student' 
class Student
{
public:
    int num;
    char name[10];
};

//Class 'Lscore' extends Student (virtual)
class Lscore : virtual public Student   
{
public: 
    int eng;    
};

//Class 'Nscore' extends Student (virtual)
class Nscore : virtual public Student
{
public:
    int math;   
};

//Class 'Totscore' extends Lscore, Nscore
class Totscore : public Lscore, public Nscore
{
public:
    Totscore();     //Constructor1
    Totscore(char name[], int num, int eng, int math); //Constructor2
    void Display();     //Print Myself
};

//Constructor1
Totscore::Totscore( )
{
}

//Constructor2
Totscore::Totscore(char name[10], int num, int eng, int math)
{       
    strcpy_s(this->name, 10, name);
    this->num = num;    
    this->eng = eng;
    this->math = math;  
}


//Print Myself
void Totscore::Display(){
    cout<<this->num<<"  "<<this->name<<"  ";
    cout<<this->eng<<"  "<<this->math<<"  "<<endl;
}


//Print Array (--- Problem Part !! ---)
void PrintArray(Totscore *stu){ 
    for(int i=0; i< MAX; i++){
        stu[i].Display();       
    }

}

//Main Function
int main(){
    Totscore *st[MAX];      //Class Array 'st'

    st[0] = new Totscore("A",101,85,77);
    st[1] = new Totscore("B",102,90,89);
    st[2] = new Totscore("C",103,80,55);
    st[3] = new Totscore("D",104,75,85);
    st[4] = new Totscore("E",105,85,85);

    PrintArray(*st);
}

And run screen is following. (I can't upload an image because my reputation is low.)

101  A  85  77

And 'Debug error message' displayed...

like image 734
NatuerConservation Avatar asked Mar 07 '26 23:03

NatuerConservation


2 Answers

You're creating an array of pointers here:

Totscore *st[MAX];

But you're passing the first element:

PrintArray(*st);

Changing your function to take a pointer to the entire array should work:

void PrintArray(Totscore **stu){ 
    for(int i=0; i< MAX; i++){
        stu[i]->Display();       
    }
}

PrintArray(st);
like image 144
Pubby Avatar answered Mar 10 '26 14:03

Pubby


Few major problems leading to undefined behavior:

  1. Default constructor of Student does not initialize its char array.
  2. Arrays passed by value into constructors.
  3. Incorrect iteration trough array. Memory layout of array of objects is different from array of pointers to objects.

Well, there is also a memory leak, many unnecessary operations just to initialize data. You should use constructor initialization lists instead. There is no strcpy_s function in C/C++, it is a terrible Microsoft invention. Why not to use portable strncpy instead?

Here is a bit improved code:

#include <cstring>
#include <iostream>

using namespace std;

#define MAX 5       //size of array

//Class 'Student' 
class Student
{
public:
    Student (int num = 0) : num (num)
    {
        name[0] = '\0';
    }

    Student (const char *name, int num = 0) : num (num)
    {
        strncpy (this->name, name, sizeof (this->name));
    }

    int num;
    char name[10];
};

//Class 'Lscore' extends Student (virtual)
class Lscore : virtual public Student   
{
public: 
    Lscore (int eng = 0) : eng (eng) {}
    int eng;    
};

//Class 'Nscore' extends Student (virtual)
class Nscore : virtual public Student
{
public:
    Nscore (int math = 0) : math (math) {}
    int math;   
};

//Class 'Totscore' extends Lscore, Nscore
class Totscore : public Lscore, public Nscore
{
public:
    Totscore() {}     //Constructor1
    Totscore(const char *name, int num, int eng, int math); //Constructor2
    void Display();     //Print Myself
};

//Constructor2
Totscore::Totscore(const char *name, int num, int eng, int math)
        : Student (name, num), Lscore (eng), Nscore (math)
{       
}


//Print Myself
void Totscore::Display(){
    cout<<this->num<<"  "<<this->name<<"  "
        <<this->eng<<"  "<<this->math<<"  \n";
}


//Print Array (--- Problem Part !! ---)
void PrintArray(Totscore **stu){ 
    for(int i=0; i< MAX; ++i){
        stu[i]->Display();       
    }

}

void DeleteArray(Totscore **stu){ 
    for(int i=0; i< MAX; ++i){
        delete stu[i];
    }
}

//Main Function
int main(){
    Totscore *st[MAX];      //Class Array 'st'

    st[0] = new Totscore("A",101,85,77);
    st[1] = new Totscore("B",102,90,89);
    st[2] = new Totscore("C",103,80,55);
    st[3] = new Totscore("D",104,75,85);
    st[4] = new Totscore("E",105,85,85);

    PrintArray(st);
    DeleteArray (st);
}