【问题标题】:c++ Pointer to another class bug "no operator matches these operands."c++ 指向另一个类错误的指针“没有运算符匹配这些操作数。”
【发布时间】:2023-03-27 15:49:01
【问题描述】:

编辑* 我很笨...感谢您帮助我解决了这个问题。 * 在我的 car 类中,下面的代码不断在 上给我一个错误

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

【问题讨论】:

  • 你称编译器错误为“错误”?

标签: c++ compiler-errors operators


【解决方案1】:
const std::string& Person::print() const
{
    return "Name " + name + "\n" + "Age " + age + "\n";    
}

【讨论】:

  • 你确定在这种情况下你可以返回引用std::string&amp;吗?
【解决方案2】:

成员函数print 返回void。您不能将 void 插入流中。如果您更改打印函数以获取流引用并添加调用 print 的重载运算符,您可以将对象本身插入流中;那会更惯用。

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

【讨论】:

    【解决方案3】:

    执行此操作的 C++ 方法是为 PersonCar 实现 std::ostream&amp;&lt;&lt; 运算符。例如,

    #include <iostream>
    
    std::ostream& operator<<(std::ostream& o, const Person& p) {
      return o << "Name: " << p.getName() << " Age: " << p.getAge();
    }
    

    然后像这样使用:

    Person p("Bob", 23);
    std::cout << p << "\n";
    

    顺便说一句,我认为您的代码中并不需要所有这些指针。

    【讨论】:

      【解决方案4】:
      cout << "Driver: " << driver->print(); 
      

      您的&lt;&lt; 正在等待适当的对象进行处理。那是你的问题。与其实现print(),不如通过实现operator&lt;&lt;(std::ostream&amp;, const Person&amp;); 来重载运算符,然后让C++ 完成剩下的工作。然后你可以这样打印:

       cout << "Driver: " << driver; 
      

      【讨论】:

      • toString()? 这不是 java。规范的方法是实现std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Person&amp;);
      • @juanchopanza 感谢提醒,我相应地更改了答案。
      • @juanchopanza:但请注意,C++11 添加了std::to_string,这实际上是有用的,至少对于某些类型而言。
      • @JerryCoffin 当然,但我更喜欢拥有输出流运算符的想法。如果它在实现中使用字符串是另一回事。
      • @juanchopanza:哦,我对此没有异议(事实上,对你的答案的赞成票之一来自我)。
      【解决方案5】:

      您的 print() 函数已经将输出发送到cout,因此无需再次尝试将其发送到那里。而不是

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

      你可能想要

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

      【讨论】:

      • 哇,谢谢。在那之后我可能应该睡一会儿了。
      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多