如何让我的重载<<运算符打印出我的函数?
How to get my overloaded << operator to print out my function?
我的 Cinema 类遇到了重载运算符的问题,该运算符应该打印出所有电影的列表、与之关联的时间列表,然后是发布日期。我想我有 printAll(( 函数可以正常工作,但我似乎在重载函数中以正确的方式打印出来时遇到了麻烦。
这是日期类
class Date
{
public:
Date(int = 0, int = 0, int = 0);
//….
// other as appropriate
bool operator < (Date&);
bool operator == (Date &);
friend ostream & operator <<(ostream &, const Date &);
private:
int day, month, year;
};
存放日期的电影类
class Movie
{
public:
Movie(string& name, int yyyy, int mm, int dd) : name(name), releaseDate(Date(yyyy, mm, dd)) {};
const Date & getReleaseDate();
// other?
bool operator < (Movie& r);
bool operator == (Movie &);
Movie & operator ++ ();
friend ostream & operator << (ostream &, Movie &);
friend ostream & operator << (ostream &, const Movie &);
private:
Movie() = default;
const Date releaseDate;
string name;
int rating;
};
Cinema 类,它调用函数 printAll((,通过和重载<<运算符,并打印出所有电影、它们的时间和它们的发布日期
class Cinema
{
public:
Cinema() = default;
Cinema(Cinema &);
void addMovie(Movie *, list<int> & );
friend ostream & operator << (ostream &, Cinema &);
//Movie * operator[](int);
void movieRunningAt(Movie &, list<int> & );
void printShowTimes(const Movie *);
void printAll();
private:
std::list<Movie *> movies;
map<const Movie*, list<int>> movie_times;
};
重载的日期运算符
ostream & operator << (ostream & os, const Date & dt)
{
os << dt.month << "/" << dt.day << "/" << dt.year << "n";
return os;
}
这就是 printAll(( 函数。
void Cinema::printAll()
{
cout << "All movies and times for Cimean";
for (auto & mov : movie_times)
{
cout << mov.first << ": ";
for (auto & tim : mov.second)
{
cout << tim << " ";
}
for (auto & li : movies)
{
if (li == mov.first)
{
cout << li->getReleaseDate << "n";
}
}
}
}
影院超载<<运算符
ostream & operator << (ostream & os, const Cinema & ci)
{
os << ci.printAll() << "n";
return os;
}
它给我的错误是
"void Cinema::printAll(void): cannot convert 'this' pointer from 'const Cimea' to 'Cimea &'"
还有
Severity Code Description Project File Line Suppression State
Error C3867 'Movie::getReleaseDate': non-standard syntax; use '&' to create a pointer to member
你需要让编译器知道printAll
不会对对象进行更改(也就是说,由 const 对象调用是件好事(。所以制作标题:
void printAll() const;
和
void Cinema::printAll() const {
第二个错误是因为您在函数调用中忘记了括号。改变
cout << li->getReleaseDate << "n";
自
cout << li->getReleaseDate() << "n";
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为什么我的 std::ref 无法按预期工作?
- clang整洁10忽略了我的NOLINT命令
- 重载& lt; & lt;为了我的班级