【发布时间】:2020-01-25 11:40:22
【问题描述】:
我已经为 struct LevelStats 实现了 operator '
头文件:
struct LevelStats
{
DIFFICULTY level;
std::chrono::duration<double> best_time;
unsigned int games_played;
unsigned int games_won;
};
std::ofstream& operator<<(std::ofstream &os, const LevelStats &stats);
cpp 文件:
std::ofstream &operator<<(std::ofstream &os, const LevelStats &stats) {
os << static_cast<unsigned int>(stats.level) << " " << "Best_Time= " << stats.best_time.count()<<std::endl;
os << static_cast<unsigned int>(stats.level) << " " << "Games_Played= " << stats.games_played<<std::endl;
os << static_cast<unsigned int>(stats.level) << " " << "Games_Won= " << stats.games_won<<std::endl;
return os;
}
这适用于像
这样的操作文件
,但当用作
std::cout
结果:
错误:无法绑定 'std::ostream {aka std::basic_ostream}' 左值到 'std::basic_ostream&&'
编辑:替换为 std::ostream& 遇到同样的错误 另一个编辑:参数中的愚蠢错误 - 它有效
【问题讨论】:
-
将所有出现的
ofstream更改为ostream。
标签: c++ struct operator-overloading std