-1

I should be overloading the operator + for adding two squads that will return as a result a new squad with the number of members that is the sum of the number of members of both squads, and other attributes are taken from the squad with larger number of members. I am trying it but the result does not make any sense! And I don't know whether the mistake is in the part where I am overloading the + operator, or it is in the function that shows which tour has most members?!

Here is what all the exercise says: "Mountain squad Problem 2 (0 / 30)

Write a class for mountain squad, that keeps information for the name of the squad (dynamically allocated array of chars), number of tours (integer) and number of members (integer). For this class implement:

•operator + for adding two squads that will return as a result a new squad with number of members that is sum of the number of members of both squads, and other attributes are taken from the squad with larger number of members.

•operators >, < for comparison by the number of members

•operator << for printing the info on SO.

Write a function that accepts array of mountain squads and the size of the array and prints the squad with max number of members. "

#include <iostream>
#include <string.h>
using namespace std;

class MSquad{
private:
char *name;
int tours;
int members;
void copy(const MSquad &toCopy){
    name = new char[strlen(toCopy.name) + 1];
    strcpy(name, toCopy.name);
    tours = toCopy.tours;
    members = toCopy.members;
}


public:
MSquad(char *n = "unknown", int nT = 0, int nM = 0){
    name = new char[strlen(n + 1)];
    strcpy(name, n);
    tours = nT;
    members = nM;
}

MSquad(const MSquad &toCopy){
    copy(toCopy);   
}
~MSquad(){
    delete [] name;
}

const MSquad &operator=(const MSquad &right){
    if(&right != this){ // avoiding self- assignment
        delete [] name;
        copy(right);
    }

    return *this;
}

MSquad &operator+(const MSquad &right) const{

    members = members + right.members;
    if(right.members > members){
        name = new char[strlen(right.name) + 1];
        strcpy(name, right.name);
        tours = right.tours;
        //members = members + right.members;
    }
    return *this;

}

bool operator>(const MSquad &right){
    return members > right.members;
}

bool operator<(const MSquad &right){
    return members < right.members;
}

friend ostream &operator<<(ostream &output, const MSquad &right);
friend void mostMembers(MSquad *squads, int size);


};

ostream &operator<<(ostream &output, const MSquad &right){
output << "Name: " << right.name;
output << " Tours: " << right.tours;
output << " Members: " << right.members << endl;
return output;
}
void mostMembers(MSquad squads[], int size){
int max = squads[0].members;
int j = 0;
for(int i = 1; i < size; i++){
    if(squads[i].members >= max){
        max = squads[i].members;
        j = i;
    }
}

cout << "The max number of members is in squad in: " << squads[j] << endl;
}


int main()
{               
MSquad squads[3];
MSquad s;
for (int i=0;i<3;i++)
{
    char name[100];
    int tours;
    int members;
    cin>>name;
    cin>>tours;
    cin>>members;       
    squads[i] = MSquad(name, tours, members);

}

s = squads[0] + squads[1];
cout<<s;

mostMembers(squads, 3);

return 0;
}
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
M.T
  • 303
  • 4
  • 15
  • The conditional in your + operator doesn't look right; the only way it can be true is if `members` is negative which looks to me that shouldn't be the case – kmdreko Mar 23 '16 at 18:44
  • 2
    Please read your requirements. You're supposed to return a brand new `MSquad` object with `operator+`, not a reference, and not a reference to the current object. Second, you need to write a copy constructor and assignment operator for your class, since you insisted on using pointers to dynamically allocated memory as members. See the [Rule of 3](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – PaulMcKenzie Mar 23 '16 at 18:44
  • "I am trying it but the result does not make any sense!" That's not enough. What's exactly your problem? Do you get any errors? – DimChtz Mar 23 '16 at 18:45
  • The assignment operator and the copy constructor are there, and how am I supposed to return a new object? I'm new to C++... – M.T Mar 23 '16 at 18:49
  • Error 1:error: read-only variable is not assignable members = members + right.members; ^ – M.T Mar 23 '16 at 18:52
  • Your `operator +` is supposed to return an `MSquad`, not `MSquad&`. You're supposed to create one from scratch, add up the contents in the scratch, and return the scratch. You don't return the current object. And why is your `operator=` returning a const object? It also has flaws, one being that you've deleted `name` without knowing if creating the new `name` will fail or not. – PaulMcKenzie Mar 23 '16 at 18:54
  • Ok, now it has no errors, but If I input this: Bistra 12 75 Kozuv 15 89 Kozjak 2 15 The output should be: Name: Vodno Tours: 5 Members: 150 The max number of members is in squad in:: Name: Vodno Tours: 5 Members: 100 – M.T Mar 23 '16 at 19:00
  • and it's not it is : Name: Vodno Tours: 5 Members: 150 The max number of members is in squad in:: Name: Vodno Tours: 5 Members: 150 – M.T Mar 23 '16 at 19:03
  • @M.Tochi See my answer -- Use the code that you *did* write to your advantage. – PaulMcKenzie Mar 23 '16 at 19:05

2 Answers2

0

The problem is that you're not understanding what operator + is supposed to return. According to your assignment:

•operator + for adding two squads that will return as a result a new squad with number of members that is sum of the number of members of both squads,

In your code, you're returning a MSquad& for operator +. This is not correct, as you should be returning a new MSquad object that is made up of the current object and the passed-in object.

However, all is not lost, and we can keep the code you have now. What's saved us is that you've written a user-defined copy constructor and destructor, so we can apply an easy fix.

The way we can fix this is to do the following (this was not in your requirements, but let's use the code you do have now):

MSquad &operator+=(const MSquad &right) const
{
    members = members + right.members;
    if(right.members > members){
        name = new char[strlen(right.name) + 1];
        strcpy(name, right.name);
        tours = right.tours;
        //members = members + right.members;
    }
    return *this;
}

MSquad operator+(const MSquad &right) const
{
   MSquad temp(*this); // create a temporary copy of the current object 
   return temp += right;  // use += above on passed-in object and return object.
}

We've taken your existing code, made it operator+=, and just created an operator + using this function. So we killed two birds with one stone. We created an operator +=, which is supposed to return a reference to the current object, and wrote operator + using operator += as a helper function. Also, note that we created the temporary of the current object by taking advantage of the copy constructor.

In general, when you overload the operators such as + it is a better practice to overload op= first, and call it from operator op.


Please note that there are many other issues with your code involving mismanagement of pointers and memory leaks. For example, the operator + function (now operator += in the answer) has a memory leak in that you didn't delete [] the previous name allocation. However I leave it up to you to fix these issues.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Okay, thank you! this is partially working, the problem now I think is with the void mostMembers(MSquad squads[], int size) function, it's supposed to print the squad with the largest number of members. The output says that the first object is always the largest, and it's not... – M.T Mar 23 '16 at 19:19
  • Well,.that is another issue. What my answer shows is basically how you should write `operator +` using what you have now, and it's just a matter of debugging the rest of the issues you have. I pointed out one, where you're not deleting the memory in your `operator +(=)` – PaulMcKenzie Mar 23 '16 at 19:22
-6

Did you try going thru with the debugger?

Or, better yet - solve your problem on paper - then translate it into the code.

Igor
  • 5,620
  • 11
  • 51
  • 103
  • Why minus? The OP says (s) he not sure what gives wrong results. So I suggested to either solve it on paper and translate the solution in the code or go with the debugger. All is pretty logical. – Igor Mar 23 '16 at 18:47
  • 2
    Still, it's not an actual answer, it's more like a suggestion or advice and common logic I could say – DimChtz Mar 23 '16 at 18:48