简体   繁体   English

c ++指向另一个类错误的指针“没有运算符匹配这些操作数。”

[英]c++ Pointer to another class bug “no operator matches these operands.”

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;
   }

The C++ way of doing this would be to implement std::ostream&<< operators for Person and Car . C ++的实现方式是为PersonCar实现std::ostream&<<运算符。 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. 顺便说一句,我认为您在代码中确实不需要所有这些指针。

Your print() function already sends the output to cout , so there is no need to try to send it there again. 您的print()函数已经将输出发送到cout ,因此无需尝试再次将其发送到那里。 Instead of 代替

cout << "Driver: " << driver->print(); 

you probably want 你可能想要

cout << "Driver:" << endl;
driver->print();

The member functions print return void . 成员函数print return void You can't insert a void into a stream. 您不能在流中插入void 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; 如果将打印功能更改为采用流参考,并添加了调用print的重载运算符,则只需将对象本身插入流中即可。 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;
}
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&); 而不是实现print() ,而是通过实现operator<<(std::ostream&, const Person&);重载运算operator<<(std::ostream&, const Person&); and let the C++ do the rest. 然后让C ++完成其余的工作。 Then you can print like that: 然后,您可以像这样打印:

 cout << "Driver: " << driver; 
const std::string& Person::print() const
{
    return "Name " + name + "\n" + "Age " + age + "\n";    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM