1

For a programming assignment, I need to create a program that uses classes, objects, and operator overloading to "marry" two people together. Here's what I have:

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

class Family{
public: 
   string name; 
   int age; 
   //An object pointer of Family to represent a spouse 
   Family * spouse; 

   /** 
   * A constructor that takes 3 arguments 
   * @param n  takes default 'unknown' 
   * @param a  takes default 18
   * @param s  takes default NULL
   */ 

   Family( string n="Unknown", int a=18, Family * s=NULL){
       name=n; 
       age=a; 
       spouse=s; 
   }

   friend void operator&(Family a, Family b) { // Marries two family objects
      Family A(a.name, a.age, &b);
      Family B(b.name, b.age, &a);
      a = A;
      b = B;
   }

   friend bool operator&&(Family a, Family b) { // Checks if two Family objects are married
      if (a.spouse == &b && b.spouse == &a) {
     return 1;
      } else {
     return 0;
      }
   }
};

int main(int argc, char** argv) {
    //Declaring an object F using a name and age=18 representing a female.
      Family F("Nicky",18);
    //Declaring an object M using a name, age =19 and spouse being the previous object
      Family  M("Nick",19,&F);

    //1pt Check if they are married or not using the operator &&
    cout << "Are they married " << (F&&M) << endl;
    //1pt Marry them to each other using the operator &
    (F & M);
    //1pt Check if they are married or not using && 
    cout << "Are they married " << (F&&M) << endl;
    // Printing the spouse of the Female 
    cout<< "The spouse of female "<< F.spouse->name<<endl; 
    // Printing the spouse of the male 
    cout<< "The spouse of male "<< M.spouse->name<<endl; 

    return 0;
}

When I check if they're married to each other using &&, it returns 0 both times. When it tries to print the name of the spouse (F.spouse->name), I get a segfault. I'm very inexperienced with pointers, but I'm reasonably sure the problem is in the & operator. I'm just not sure what's going wrong.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Blucario
  • 13
  • 3
  • 4
    `void operator&(Family a, Family b)` should take its arguments by reference as you want to mutate them. – Jarod42 Apr 09 '20 at 17:19
  • 1
    Helpful reading: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – user4581301 Apr 09 '20 at 17:20
  • and then it should not create new objects. Btw `Family` is not the best name for one member of a family. Names are important – 463035818_is_not_an_ai Apr 09 '20 at 17:20
  • And `operator &&` by const reference as you check addresses. – Jarod42 Apr 09 '20 at 17:20
  • You indicate a question is answered/solved by accepting an answer (which you've done). No need to edit that info into the question. – 1201ProgramAlarm Apr 09 '20 at 17:45

1 Answers1

1
friend void operator&(Family a, Family b) { // Marries two family objects
      Family A(a.name, a.age, &b);
      Family B(b.name, b.age, &a);
      a = A;
      b = B;
   }

you are setting the objects that copied from sent arguments to parameter.

#include <iostream>

class Family {
public:
    Family() {
        std::cout << "default ctor\n";
    }
    Family(const Family &) {
        std::cout << "copy ctor\n";
    }
};

void foo(Family a) {
    std::cout << "address of parameter " << &a << "\n";
}

int main() {
    Family f;
    std::cout << "address of real object " << &f << "\n";
    foo(f);
}

it outputs

default ctor
address of real object 0x7ffee1255928
copy ctor
address of parameter 0x7ffee1255920

As you can see these are different objects. First Family object has been created with

Family f;

after that we write the address of f's address and when we sent as an argument to foo function as parameter to this object. we can see copy ctor is working.

friend void operator&(Family &a, Family &b) { // Marries two family objects
      Family A(a.name, a.age, &b);
      Family B(b.name, b.age, &a);
      a = A;
      b = B;
   }

You should use L value reference to set this object.

  • That worked! Thank you very much, I'll try to remember to pass by reference for this sort of thing from now on. – Blucario Apr 09 '20 at 17:35
  • @Blucario Your welcome! Also if you are not setting the object from that you got from parameter ```friend bool operator&&(Family a, Family b)``` like in this function you have to care about const correctness also you can send those as reference too. That will save you from copying cost. ```friend bool operator&&(const Family &a, const Family &b)``` much better! – Ahmet İbrahim AKSOY Apr 09 '20 at 17:39