• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Add objects to a vector pointed to by another object of the same class

April 10, 2020 by

Questions › Add objects to a vector pointed to by another object of the same class
0
Vote Up
Vote Down
Garmaine asked 4 years ago

As part of a C++ assignment to learn more about pointers and objects, I have a class that represents family members. One of the parameters is a vector pointer, "kids", which should contain objects of that same class. I've been told to use operator overloading with '<' to add newly-created objects to the 'kids' field of another object. Here's what I currently 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 vector pointer of Family to represent children 
   vector<Family>* kids;

   /** 
   * A constructor that takes 4 arguments 
   * @param n  takes default 'unknown' 
   * @param a  takes default 18
   * @param s  takes default NULL
   * @param v  takes default NULL
   */ 
   Family( string n="Unknown", int a=18, Family * s=NULL,vector<Family> * v=NULL){
       name=n; 
       age=a; 
       kids=new vector<Family>; 
       spouse=s; 
   }
   /**2pts
    * Create a method that overloads < 
    * The method will add a Family object to the list of children
    * @param a Family object 
    */
   int kCount = 0;
   void operator<(Family f) {
       (*kids)[kCount] = f;
   }
};
int main(int argc, char** argv) {
    //Declaring an object F using a name and age=35 representing a female.
      Family F("Nicky",35);
    //Declaring an object M using a name, age =39 and spouse being the previous object
      Family  M("Nick",39,&F);

    Family c0("Ricky", 15);
    Family c1("Bicky", 12);
    Family c2("Dicky", 9);
    Family c3("Micky", 6);
    //2pts Add the kids to M using the operator <
    M < c0; 
    return 0;
}

When I attempt to run this, I get a segfault. I'm still very inexperienced with pointers, so I really don't know how to resolve this.

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, operator-overloading, pointers, vector

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023