我尝试运行的代码:
std::string genBlankName(std::vector<Post> &posts)
{
std::string baseName = "New Post ";
int postNum = 1;
for (std::vector<Post>::iterator currentPost = posts.begin(); currentPost != posts.end(); currentPost++)
{
if (posts[currentPost].name.substr(0, baseName.length()) == baseName &&
utils::is_num(posts[currentPost].name.substr(baseName.length(), std::string::npos)) &&
utils::to_int(posts[currentPost].name.substr(baseName.length(), std::string::npos)) > postNum)
{
postNum = utils::to_int(posts[currentPost].name.substr(baseName.length(), std::string::npos));
}
}
return baseName + utils::to_string(postNum);
}
我得到的错误:
/home/brett/projects/CLPoster/CLPoster-build-desktop/../CLPoster/item.h:240: error: no matching function for call to
std::vector<cl::Post, std::allocator<cl::Post> >::at(__gnu_cxx::__normal_iterator<cl::Post*, std::vector<cl::Post, std::allocator<cl::Post> > >&)
抱歉没有多说,但我假设这是一件很常见的事情,我只是不知道自己是个小问题。我会用谷歌搜索它,但它似乎是一个太普遍的问题,无法找到任何有用的东西,因为我怀疑它更多的是我的实现问题或类似的问题。
最佳答案
订阅需要使用索引,您正在使用迭代器。
您根本不需要下标,只需取消引用您的迭代器即可:
currentPost->name.substr(0, baseName.length())
……等等。
关于c++ - 不明白为什么不允许我为这个 vector 下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4970043/