0

I am trying to return an object array and assign to another object array in main function.

Function:

Student* readFromFile( string fileName ){
    Student a[SIZE];
    int x;
    int y;
    double z;
    ifstream dosya(fileName);
    for (int i = 0; i < SIZE; i++) {
        dosya >> x >> y >> z;
        Student s(x, y, z);
        a[i] = s;
    }
    return a;
}

In main:

Student* students;
students = readFromFile( "Students");
printStudents( students );

And my header:

class Student{
public:
    Student(){};
    Student( int v, int m, double k ){
        studentID = v;
        year = m;
        GPA = k;
    }
    int getstudentID(){
        return studentID;
    }
    int getYear(){
        return year;
    }
    double getGPA(){
        return GPA;
    }

private:
    int studentID; 
    int year; 
    double GPA;
};

When I run the program it returns null values, but if I print a[SIZE] it returns quite values. I cannot change the header file. What am I doing wrong?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
ktakon
  • 27
  • 5
  • A C programmer who hasn't *quite* fully moved to C++ is known as a C+ programmer, you don't want to become one of those since it just looks like a typo on your CV :-) Therefore my advice is to *embrace* C++, and start using `std::vector` - things will become *so* much easier. – paxdiablo May 03 '20 at 02:23
  • 1
    *I am trying to return an object array and assign to another object array in main function* -- Given your description, use `std::array a;` instead of pointers. That's the purpose of `std::array` so that it can be easily passed/returned between functions and assigned. – PaulMcKenzie May 03 '20 at 02:24

0 Answers0