我正在使用 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++错误信息的屏幕:
最佳答案
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/