I can't find the bug in my code. The error that pops up says "Expression: invalid null pointer".
Visual studio isn't giving me any line to go to or really any help except once I run the program the error above pops up. The weird thing is everything compiles correctly with no strange messages. Hopefully someone can help!

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Person
{
public:
    Person(string _name,int _age)
    {
        //init all variables
        name = _name;
        age = _age;
    }
    string getName()
    {
        return name;
    }
    int getAge()
    {
        return age;
    }
    int incrementAge()
    {
        age +=1;
        return age;
    }
    void print()
    {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;

    }


private:
    string name;
    int age;

};

class Car
{
public:
    Car (string m)
    {
        model = m;//int
    }
    void setDriver(Person *d)
    {
        *driver = *d;
    }
    void setOwner(Person *o)
    {
        *owner = *o;
    }
    void print()
    {
        //outputing all info
        cout << "Model: " << model << endl;
        cout << "Driver: ";
        driver->print();
        cout << "Owner: ";
        owner->print();
    }




private:

    string model;
    Person *owner;
    Person *driver;


};

int main()
{
    vector<Person*>people;
    vector<Car*>cars;
    string name = "";
    int age = 0;
    string model = 0;
    int sentValue = 0;
    int personNum = 0; //keeps cout of where in people vector we are
    while (sentValue != -1)
    {
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter age: ";
        cin >> age;
        people.push_back(new Person(name, age));
        cout << "Enter car model: ";
        cin >> model;

        Car *carPtr = new Car(model);
        carPtr->setDriver(people[personNum]);
        carPtr->setOwner(people[personNum]);
        personNum +=1;//increment
        carPtr->print(); //output all of the information

        cout << "Press -1 to stop, or 1 to enter info for others: ";
        cin >> sentValue;//allows user to stop loop of asking questions

    }//end while



    system("pause");
    return 0;
}

Here's what happens when I compile:

1> All outputs are up-to-date.
1>Manifest:
1> All outputs are up-to-date.
1>LinkEmbedManifest:
1> All outputs are up-to-date.
1> myname_lab01.vcxproj -> C:\Users\ james\Desktop\james_lab01\Debug\james_lab01.exe
1>FinalizeBuildStatus:
1> Deleting file "Debug\james_lab01.unsuccessfulbuild".
1> Touching "Debug\james_lab01.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.92
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 5 Replies

Just because the build succeedes doesn't mean the program works correctly. Use your compiler's debugger to find out where the problem is.

The problem is line 84 -- I found it in less than 1 minute with the debugger.

[edit]I see you edited your post to correct that problem after I copied it.

Well line 86 is going to have the same problem.

You will also find that your setDriver() and setOwner() functions will cause a crash. To fix this you can do the following.

//called in main()
carPtr->setDriver(people[personNum]);
carPtr->setOwner(people[personNum]);

//in Car class
void setDriver(Person *d)
{
    driver = d;
}
void setOwner(Person *o)
{
    owner = o;
}

You should be careful not to leak memory. The Car pointer that you new on line 99 goes out of scope on line 108 without being deleted. This means that you leak a Car every time you go round the while loop.

In C++ there are so-called smart pointers that can help you with this. The old one is called std::auto_ptr, but if you're using C++11 you will be able to use std::unique_ptr and std::shared_ptr. If none of this makes any sense to you then don't worry about it, but maybe look it up :)

Thanks guys. I'm new to programming and pointers so still getting used to it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.