-2

I have a Student class which has dynamic int array marks and int variable marksCount. I am trying to overload operator+ to sum 2 Student* objects. It is supposed only to sum their marks.

For example, when adding 2 students (Student* objects) with marks { 2, 3, 4 } + { 1, 5, 2 }, I must have an output of Student* with marks { 3, 8, 6 }.

But my solution throws C++ expression must be an unscoped integer or enum type exception on

Student* s = s1 + s2; line.

I have already read similar question and answers but unfortunately I haven't become with solution to my problem. Maybe there is something I don't know about how operator+ works?

#include <iostream>

using namespace std;

class Student
{
private:
    int* marks;
    int marksCount;

public:
    Student(int* marks, int marksCount)
    {
        this->marks = marks;
        this->marksCount = marksCount;
    }
    Student* operator+(Student* st)
    {
        int* marks = new int[5];
        for (int i = 0; i < 5; i++)
        {
            marks[i] = this->marks[i] + st->getMarks()[i];
        }

        Student* s = new Student(marks, 5);

        return s;
    }

    int* getMarks()
    {
        return marks;
    }
    void setMarks(int* marks, int SIZE)
    {
        for (int i = 0; i < SIZE; i++)
        {
            this->marks[i] = marks[i];
        }
    }
    int getMarksCount()
    {
        return marksCount;
    }
    void setMarksCount(int count)
    {
        marksCount = count;
    }

    static void printMarks(Student* s1)
    {
        for (int i = 0; i < s1->getMarksCount(); i++)
        {
            cout << s1->getMarks()[i];
        }
    }
};

int main()
{
const int SIZE = 5;
int* marks1 = new int[SIZE] { 2, 3, 5, 4, 1 };
int* marks2 = new int[SIZE] { 4, 2, 3, 1, 2 };

Student* s1 = new Student(marks1, SIZE);
Student* s2 = new Student(marks2, SIZE);

Student* s = s1 + s2;


Student::printMarks(s1);
}
  • 2
    The problem here is called "pointless use of pointers". Nothing in the shown code requires any kind of a pointer. If you simply get rid of all pointers, and use `std::vector` only where it's needed, all these problems will go away by themselves! – Sam Varshavchik Jun 02 '22 at 12:13
  • C++ is not Java. As mentioned, there is no need for `new` or `new[]` anywhere in your program. And even when you did that, nowhere do I see a single `delete` or `delete[]` being used. Your program is full of memory leaks. If you truly want to write a class that uses a pointer and dynamically allocated memory, you can't do a half-done job as you've done. You are missing a user-defined destructor, copy constructor, and assignment operator to handle the dynamically allocated member. – PaulMcKenzie Jun 02 '22 at 12:48
  • @PaulMcKenzie, this code is only is small practice in overloading operators. I used dynamically allocated objects because I must use them in the task I am preparing for. But what is wrong with assignment operator? I assign `Student*` pointer to another one, don't I? – Дмитро Піскарьов Jun 02 '22 at 13:21
  • @ДмитроПіскарьов First thing is that just because you must use pointers doesn't mean you use pointers everywhere and anywhere. I would have expected pointers to be used *within* the Student class. Start with that -- remove *all* pointers from your code *except* for `marks`. Then return `Student` objects, not `Student *` from the overloaded functions. Once you do that, you now have what you are required, and still use pointers. But still, you would need to write a user-defined copy, assignment, and destructor for the `Student` class. – PaulMcKenzie Jun 02 '22 at 13:25
  • `Student operator+(const Student& st)` -- If your final code doesn't look something like that, you are doing everything wrong (or weird), and not using C++ operator overloading properly and idiomatically. You can still have `int *marks;` (your requirement for pointers), but still have something that looks like a coherent C++ program, and not a hodgepodge of unnecessary pointer declarations. – PaulMcKenzie Jun 02 '22 at 13:35

1 Answers1

0

You don't need to use new or pointers here

Student* s1 = new Student(marks1, SIZE);
Student* s2 = new Student(marks2, SIZE);

Student* s = s1 + s2;

so instead

Student s1{marks1, SIZE};
Student s2{marks2, SIZE};

Student s = s1 + s2;

Similarly I would change all your int* to std::vector<int> and basically remove all of the new calls in your entire code.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • This code was only a small practice in operator overloading. I have used pointers because I must use dynamic array in the task I am preparing for. Is there any way to use it with pointers? – Дмитро Піскарьов Jun 02 '22 at 13:13
  • You can keep the dynamic arrays if you must, but that is unrelated to the incorrect use of pointers for `Student` it is really non-idiomatic for `operator+` to create a new object using `new`. – Cory Kramer Jun 02 '22 at 13:34
  • @ДмитроПіскарьов • *I must use dynamic array* In C++, the dynamic array is `std::vector`. – Eljay Jun 02 '22 at 15:03