我不知道为什么cout << da << '\n'工作正常,但出std::cout << next_Monday(da) << '\n'了问题.为什么直接Date对象可以输出,但返回Date不能.为什么operator <<有时超载工作,但有时则不工作.这是我的代码..
#include <iostream>
#include <stdlib.h>
struct Date {
unsigned day_: 5;
unsigned month_ : 4;
int year_: 15;
};
std::ostream& operator<<(std::ostream& out,Date& b)
{
out << b.month_ << '/' << b.day_ << '/' << b.year_;
return out;
}
std::istream& operator>>(std::istream& in,Date& b);
Date next_Monday(Date const &d);
int main()
{
Date da;
std::cin >> da;
std::cout << da << '\n';
std::cout << …Run Code Online (Sandbox Code Playgroud) 我尝试创建与一起使用的自定义类std::set。我知道我需要为此提供一个自定义比较器,因此我使过载operator<。但是,当我尝试使用代码复制集时set<Edge> a; set<Edge> b = a;,出现以下错误:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__functional_base:63:21:对二进制表达式无效的操作数(“ const Edge”和“ const Edge”)
class Edge {
public:
Edge(int V, int W, double Weight):v(V),w(W),weight(Weight){}
int other(int vertex){ return v ? w : vertex == w;}
int v,w;
double weight;
friend std::ostream& operator<<(std::ostream& out, const Edge& e)
{
out<<e.v<<' '<<e.w<<' '<<"weight:"<<e.weight<<'\n';
return out;
}
bool operator<(const Edge& other)
{
return weight < other.weight;
}
};
Run Code Online (Sandbox Code Playgroud)