I am having a little trouble with my class Person program. It runs and builds without error...Yay....except I am getting 0 output. Anyone have any advice?

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

class Person{
    string name;
    int age;

    Person (string n, int a); //n=name, a=age
    string get_name() const;
    int get_age() const;
    void increment_age();
    void print()const;

Person::Person (string n, int a){
name = n;
age = a;

string Person::get_name() const{
return name;

void Person:: increment_age(){
age +=1;

void Person::print() const{
cout << name << endl;
cout << age << endl;

class Car{
    string model;
    Person *owner;
    Person *driver;

    Car (string m);
    void set_driver (Person* p);
    void set_owner (Person* p);
    void print() const;


Car::Car (string m){
model = m;

void Car:: set_driver (Person* p){
driver = p;

void Car:: set_owner (Person* p){
owner = p;

void Car::print() const{
cout <<  model << endl;
cout << driver->get_name() << endl;
cout << owner->get_name() << endl;

int main(){

vector<Person*> people;

const int PERSON_SZ = 3;
char * names[] = { "Todd", "Melissa" "Ashley" };
int ages[] = { 43, 33, 22 };

for ( int i = 0; i<PERSON_SZ; i++){
    Person *a = new Person ( names[i], ages[i]);

vector<Car*> cars;

const int CAR_SZ =3;
char * models[] = { "Suburban", "Ford Explorer", "Honda" };

for ( int i=0; i<CAR_SZ; i++){
    Car *c = new Car (models[i]);
    c-> set_driver ( people[rand()% (people.size())] );
    c->set_owner ( people [rand()% (people.size()) ] );

for (int i=0; i<CAR_SZ; i++){
    std::vector<Car*> print();

return 0;
3 Years
Discussion Span
Last Post by rubberman

The line 99 won't do what you think it does (whatever that is). This line:

std::vector<Car*> print();

actually declares a function called "print" that returns a vector of Car pointers. In other words, the line does not do anything concrete.

I believe that what you meant to write was this:

for (int i=0; i<CAR_SZ; i++){

which calls the print function on all the car objects.

Also, you should get into the habit of cleaning up after your code, to not let memory leak (here, it doesn't matter, but latter it might). So, you should clean up your allocations (at the end, before the return 0; line):

for (int i=0; i < CAR_SZ; i++){
    delete cars[i];

for (int i=0; i < PERSON_SZ; i++){
    delete people[i];

Thank you! Your awesome it worked. I originally had put in the first print out you told me, but the compiler was telling me I wasn't calling a print function. So I changed it. The "clean up" worked. Thanks again!


Keep up your efforts Mell. Someday you may become a competent coder! :-) We all learn from our mistakes, and it seems you are learning rapidly! Tongue-in-cheek humor here. Keep posting, and we'll keep trying to help you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.