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 ==========