gpt4 book ai didi

c++ - 自定义类集的重载提取运算符

转载 作者:行者123 更新时间:2023-11-28 00:25:43 26 4
gpt4 key购买 nike

我做了一个自定义类 user ,并做了一个set<user> users在另一个类(class) dbase .我已经成功地重载了 user 类的提取运算符现在我正在尝试重载 dbase 类的提取运算符.

这是我的代码:

ostream& operator<< (ostream &out, dbase &db) {
set<user>::iterator it;
for(it=db.users.begin(); it!=db.users.end(); it++)
out<<(*it)<<endl; //error reported on this line
return out;
}

但我一直收到这个错误:

error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

这是 operator<<类声明user :

ostream& operator<<(ostream &out, user &u) {
cout<<endl;
cout<<"Username: "<<u.uname;
cout<<endl;
return out;
}

这是完整的代码:(ideone)

#include <iostream>
#include <set>
#include <string>
#include <sstream>
using namespace std;

template <class T>
string to_string(T x) {
stringstream ss;
ss<<x;
return ss.str();
}

class date {
int dd,mm,yyyy;
string str_date;
public:
string get_date() {
return to_string(dd)+"-"+to_string(mm)+"-"+to_string(yyyy);
}
friend ostream& operator << (ostream&,date&);
friend istream& operator >> (istream&,date&);
};

ostream& operator<< (ostream &out, date &d) {
out<<d.get_date();
return out;
}

istream& operator>> (istream &in, date &d) {
cin>>d.dd>>d.mm>>d.yyyy;
return in;
}

class user {
string uname, pwd;
string full_name, contact;
static unsigned long long int id;
date dob;

public:
bool operator < (const user &u) const {
if(uname.compare(u.uname)<0) return true;
return false;
}
void set_uname(string u) {
uname = u;
}
void set_attrib() {
string temp_pass;
while(1) {
cout<<"Full Name: ";
cin>>full_name;
cout<<"Password: ";
cin>>temp_pass;
cout<<"Confirm Password: ";
cin>>pwd;
if(pwd.compare(temp_pass))
cout<<"Passwords do not match. Try Again."<<endl;
else break;
}
cout<<"Contact Number: ";
cin>>contact;
cout<<"Date of Birth(dd mm yyyy): ";
cin>>dob;
}
friend ostream& operator<<(ostream&, user&);
};

ostream& operator<<(ostream &out, user &u) {
cout<<endl;
cout<<"Username: "<<u.uname;
cout<<endl;
return out;
}

class dbase {
set<user> users;
public:
void add_user() {
string uname;
user u;
while(1) {
cout<<"Username: ";
cin>>uname;
u.set_uname(uname);
if(users.find(u)==users.end()) {
u.set_uname(uname);
u.set_attrib();
users.insert(u);
break;
}
else
cout<<"Username already exists. Please try a different username."<<endl;
}
}
friend ostream& operator << (ostream&, dbase&);
};

ostream& operator<< (ostream &out, dbase &db) {
set<user>::iterator it;
user u;
for(it=db.users.begin(); it!=db.users.end(); it++)
out<<(*it)<<endl;
return out;
}

int main() {

return 0;
}

最佳答案

所以...

第 1 课

请发布一个完整的可编译示例。在这种情况下,您没有提供足够的信息让我们轻松调试您的代码。

对于您的特定问题,该代码可能如下所示:

#include <iostream>
#include <set>
using namespace std;

class user {
public:
user(int id = 0): id(id) {}
int id;
};

class dbase {
public:
set<user> users;
};

bool operator<(user const & lhs, user const & rhs) {
return lhs.id < rhs.id;
}

ostream& operator<< (ostream &out, user & user) {
return out << "user(" << user.id << ")";
}

ostream& operator<< (ostream &out, dbase &db) {
set<user>::iterator it;
user u;
for(it=db.users.begin(); it!=db.users.end(); it++)
cout<<*it<<endl;
return out;
}

int main() {
dbase db;
db.users.emplace(3);
db.users.emplace(4);
cout << db << endl;
}

第 2 课

请发布您的编译器报告的完整错误消息。在这种情况下,它实际上是:

se.cc: In function ‘std::ostream& operator<<(std::ostream&, dbase&)’:
se.cc:28:16: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
cout<<*it<<endl;
^
In file included from /opt/gcc/4.8.1/include/c++/4.8.1/iostream:39:0,
from se.cc:1:
/opt/gcc/4.8.1/include/c++/4.8.1/ostream:602:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = user]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^

请注意,我没有以任何方式编辑错误消息。消息中可能有您遗漏的其他信息,这些信息可能对尝试回答您问题的人有所帮助。

第 3 课

代替:

ostream& operator<< (ostream &out, user & user) { ... }

做:

ostream& operator<< (ostream &out, user const & user) { ... }

第 4 课

当重载流提取或流插入运算符时,您可能永远不应该使用 coutcerr .相反,您已将一个流对象传递到您的函数中。在您的情况下,您已使用 out 的典型(预期)参数名称命名它.

因此,您的两个流插入运算符应该具有 cout 的每个实例替换为 out .

这将导致您的代码在您扩展代码以使用诸如使用 fstream 将用户数据库写入文件,或者如果您想将消息打印到 cerr正确 工作。

第 5 课

只是要注意。这是一个问题,因为 *it返回对 const User 的引用.所以你现有的 operator<<重载不匹配,因为它需要用户的非常量版本。

关于c++ - 自定义类集的重载提取运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25246140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com