如何让我的重载<<运算符打印出我的函数?

How to get my overloaded << operator to print out my function?

本文关键字:我的 lt 函数 打印 运算符 重载      更新时间:2023-10-16

我的 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";