Reputation: 391
edit* I'm dumb... Thanks for helping me this question is solved. * In my car class the code below keeps giving me an error on the << operators
cout << "Driver: " << driver->print();
cout << "Owner: " << owner->print();
The error says "no operator matches these operands." This is my homework assignment so I do need to somehow from the driver call the print function. In the main function I'm not actually setting the driver or owner yet but that shouldn't matter I wouldn't think. Thanks in advance.
#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;
}
Upvotes: 0
Views: 1476
Reputation: 1
const std::string& Person::print() const
{
return "Name " + name + "\n" + "Age " + age + "\n";
}
Upvotes: 0
Reputation: 76438
The member functions print
return void
. You can't insert a void
into a stream. If you change the print functions to take a stream reference and add overloaded operators that call print, you can just insert the objects themselves into the stream; that would be more idiomatic.
template <class Elem, class Traits>
void Car::print(std::ostream<Elem, Traits>& out) {
out << "Model: " << model << '\n';
out << "Driver: " << *driver;
out << "Owner: " << *owner;
}
template <class Elem, class Traits>
std::ostream<Elem, Traits>& out, const Car& car) {
return out << car;
}
Upvotes: 1
Reputation: 227468
The C++ way of doing this would be to implement std::ostream&<<
operators for Person
and Car
. For example,
#include <iostream>
std::ostream& operator<<(std::ostream& o, const Person& p) {
return o << "Name: " << p.getName() << " Age: " << p.getAge();
}
then use like this:
Person p("Bob", 23);
std::cout << p << "\n";
As an aside, I don't think you really need all those pointers in your code.
Upvotes: 2
Reputation: 3486
cout << "Driver: " << driver->print();
your <<
waiting for an appropriate object to process. That is your problem. Rather than implementing print()
, overload the operator by implementing operator<<(std::ostream&, const Person&);
and let the C++ do the rest. Then you can print like that:
cout << "Driver: " << driver;
Upvotes: 0
Reputation: 1419
Your print() function already sends the output to cout
, so there is no need to try to send it there again. Instead of
cout << "Driver: " << driver->print();
you probably want
cout << "Driver:" << endl;
driver->print();
Upvotes: 1