c++ - 错误 : no match for operator<<

标签 c++ struct

我正在使用 C++ 和类(class)进行培训。我的项目由两个文件组成:

  • main.cpp
  • 日期.h

main.cpp

using namespace std;

#include <iostream>
#include<Windows.h>
#include  "Date.h"

main(){
  Date date1;
  Date date2;
  cout<<"Type first date: ";
  date1.setAll();
  cout<<"type second date: ";
  date2.setAll();

  cout<<First date: "<<date1.getS();
  cout<<Second date: "<<date2.getS();
}

Date.h

class Date{
public:
  Date(){}
  ~Date(){system("pause");}
  void setAll();
  struct dmy{
    int day, month, year;
  };
  dmy c;
  dmy getS();
private:
  void setDay();
  void setMonth();
  void setYear();
};

void Date::setAll(){
  setDay();
  setMonth();
  setYear();
}

//all set ...

Date::dmy Date::getS(){
  return c;
}

我在 main 中遇到错误

cout<<"First date: "<<date1.getS();

错误信息以

开头
Error: no match for 'operator<<' in std::operator<<

这个错误是什么意思,我该如何解决?

由于我们学校电脑的网络限制,我不能直接复制错误信息,这里是DevC++错误信息的屏幕:

errors

最佳答案

ostream类没有 operator<<为您的 struct dmy 重载.因此它不知道如何打印该值。在不重载运算符的情况下,您需要的是这样的:

Date::dmy date = date1.getS();
cout<<"First date: "<< date.year << "." << date.month << "." << date.day;

关于c++ - 错误 : no match for operator<<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068040/

相关文章:

c++ - 在 Code::Blocks 中调试我的 C++ 项目时出现 "Program received signal SIGSEGV, Segmentation fault. In ?? () ()"

c++ - 优化器是否将临时非 POD 类型移出循环?

c++ - Qt 5.1 qHash 错误

c - 如何将邻接表添加到 C 中已定义类型的节点?

arrays - Swift 结构错误

c - 在 C 中使用 union/结构查找内存中的起始地址

c++ - IcmpSendEcho 返回 183

c++ - 从文件中读取数据

c - 结构单向链表。为什么这不起作用?

c - 将带有 typedef 的结构数组传递给函数