我在我的程序中使用 std::vector 编译时出现此错误:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:在成员函数'User& User::operator=(const User&)'中:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238:从
void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = User, _Alloc = std::allocator<User>]' /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:564: instantiated from
void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = 用户,_Alloc = std::allocator]'main.cpp:100:从这里实例化
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238:错误:非静态常量成员`const std::string User::NAME',可以' t 使用默认赋值运算符
我有“用户”和“用户”类:
class User {
public:
const string NAME;
User(const string &name, const bool &isMain = false) : NAME(name), isMain(isMain) {};
void addFollower(User * user);
void addReplier(User * user, const int &count);
void addMentioner(User * user, const int &count);
void addRetweeter(User * user, const int &count);
private:
vector<User *> followedBy, repliedBy, mentionedBy, retweetedBy;
vector<int> replyCount, mentionCount, retweetCount;
bool isMain;
};
class Users {
public:
vector<User> users;
void addUser(const string &name, bool isMain = false);
User * findUser(const string &name);
friend ostream & operator <<(ostream &outStream, const Users &users);
User & operator [] (unsigned int index);
};
事情就是这样。错误是由方法Users::addUser()引起的:
void Users::addUser(const string &name, bool isMain) {
User newUser(name, isMain);
users.push_back(newUser);
}
如果我删除第二行
users.push_back(newUser);
它工作得很好,但是,它不像你看到的那样工作,因为它阻止我将新记录添加到“用户”向量数组中。如果有人告诉我是什么原因,我将不胜感激。谢谢