【发布时间】:2018-09-19 06:10:34
【问题描述】:
我正在尝试使用 setter 函数将对象添加到向量中。我的文件如下
#ifndef ROOM_H
#define ROOM_H
#include <string>
#include <vector>
#include "User.h"
using namespace std;
class Room {
vector<User> users;
public:
Room();
void addToRoom(User user);
vector<User> getUsers();
};
#endif
addToRoom 只是
users.push_back(user);
我的 user.h 是
#ifndef USER_H
#define USER_H
#include <string>
#include <vector>
using namespace std;
class User {
string password;
string username;
public:
User(string user, string pass);
string getPass();
string getName();
};
#endif
我正在努力
void
IRCServer::addUser(int fd, const char * user, const char * password, const char * args)
{
string myUsername = user;
string myPassword = password;
User *newUser = new User(myUsername, myPassword);
Room *newRoom = new Room();
newRoom->addToRoom(newUser);
return;
}
但是,如果我传入 newUser,我会收到一条错误消息,指出没有匹配的函数,参数 1 没有从“用户*”到“用户”的已知转换。传递 &newUser 表示参数 1 没有从“用户**”到“用户”的已知转换。我需要改变我的载体,还是有其他方法可以做到这一点?
【问题讨论】:
-
你可以直接使用
User newUser(myUsername, mypassword)而不是使用new。 -
顺便说一句,
addUser(User user)是比addToRoom(User user)更好的函数名称。 -
您可能希望通过引用传递
User以防止制作不必要的临时副本。