简体   繁体   English

C ++默认副本构造函数不可行

[英]C++ Default Copy Constructor Not Viable

I have 2 classes, User , and BBoard (Bulletin Board). 我有2个类, UserBBoard (公告板)。

I have a member function in BBoard , that checks reads data from a file and pushes it back to the vector userList . 我在BBoard有一个成员函数,该函数检查从文件中读取数据并将其推回到向量userList I also have a member function that checks if a User exists (if the User exists, I want to use the default copy constructor and assign it to currentUser ) 我还有一个成员函数,用于检查User存在(如果User存在,我想使用默认的复制构造函数并将其分配给currentUser

These are the private variables I have in BBoard: 这些是我在BBoard中拥有的私有变量:

// private:
//   std::string title;
//   std::vector<User> userList;
//   User currentUser;
//   std::vector<Message> messageList;

This is the member function in BBoard that pushes back User s: 这是BBoard中的推回User的成员函数:

while (inFS >> dataName && inFS >> dataPass) {
  User x(dataName, dataPass);
  userList.push_back(x);
}

This is the member function in BBoard that checks if a User exists and attempts to use the default copy constructor to assign it to currentUser . 这是BBoard中的成员函数,用于检查User存在,并尝试使用默认的复制构造函数将其分配给currentUser

bool BBoard::userExists(const string& uName, const string& uPass) const {
  for (int i = 0; i < userList.size(); ++i) {
    if (userList.at(i).getUsername() == uName) {
      if (userList.at(i).check(uName, uPass)) {
        currentUser = userList.at(i);    //<-- HERE
        return true;
      }
    }
  }
  return false;
}

The error I am getting is this: 我得到的错误是这样的:

BBoard.cpp:153:21: error: no viable overloaded '='
        currentUser = userList.at(i);
        ~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~
./User.h:8:7: note: candidate function (the implicit copy assignment operator) not viable:
      'this' argument has type 'const User', but method is not marked const
class User {

I did not declare a copy constructor in User , so I expected the implicitly created one to work. 我没有在User声明一个副本构造函数,因此我希望隐式创建的副本构造函数能够正常工作。 I did not push back a const User either. 我也没有推回const User

According to https://en.cppreference.com/w/cpp/language/copy_constructor implicit copy constructors have this form: T::T(const T&) so I'm not sure where I went wrong. 根据https://en.cppreference.com/w/cpp/language/copy_constructor的指示,隐式副本构造函数的格式为: T::T(const T&)所以我不确定出错的地方。

I can provide any other portions of the program. 我可以提供该程序的任何其他部分。

This has nothing to do with any copy constructor. 这与任何副本构造函数无关。

bool BBoard::userExists(const string& uName, const string& uPass) const

This defines a const class method. 这定义了const类方法。 That's what the trailing const keyword, at the end of the declaration, means. 这就是声明末尾的const关键字的含义。 A const class method cannot modify any member of its class, they're all effectively const to the method (unless explicitly declared mutable , which is not the case here). const类方法不能修改其类的任何成员,它们都是有效地const的方法(除非显式声明为mutable ,此处不是这种情况)。

currentUser = userList.at(i);

This attempts to modify a member of the class. 这试图修改该类的成员。 Since it is effectively a const class member, it cannot be modified. 由于它实际上是const类成员,因此无法对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM