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);
}