编辑*我很笨...感谢您帮助我解决了这个问题。* 在我的汽车类中,下面的代码不断在 << 运算符上给我一个错误
cout << "Driver: " << driver->print();
cout << "Owner: " << owner->print();
错误提示“没有运算符与这些操作数匹配”。这是我的家庭作业,所以我确实需要从驱动程序中以某种方式调用打印功能。在主要功能中,我实际上还没有设置驱动程序或所有者,但我认为这无关紧要。提前致谢。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person
{
public:
Person(string _name,int _age)
{
_name = name;
_age = age;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
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;
}
void setDriver(Person *d)
{
*driver = *d;
}
void setOwner(Person *o)
{
*owner = *o;
}
void print()
{
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;
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;
cars.push_back(new Car(model));
cout << "Press -1 to stop, or 1 to enter info for others: ";
cin >> sentValue;
}
//display car model,
//owner’s name and age,
//and driver’s name and age.
system("pause");
return 0;
}