Reputation: 3537
The code I'm trying to run:
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);
}
And the error I get:
/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> > >&)
Sorry for not saying more, but I'm assuming this is a pretty common thing I'm just unaware of being a nub. I'd google it, but it seems too general of a problem to turn up anything useful as I suspect it's more of a problem with my implementation or something along those lines.
Upvotes: 2
Views: 313
Reputation: 131829
You don't use iterators on subscripts. Just take a size_t
for (size_t currentPost = 0; currentPost < posts.size(); ++currentPost)
Or dereference the iterator:
currentPost->name.substr(0, baseName.length())
Upvotes: 6
Reputation: 5187
vector operator[] takes size_type
as parameter and not std::vector::iterator
type.
Upvotes: 1
Reputation:
std::vector<typename T>
is a random access container, however, you have to use offset to access elements at given positions. For example, if you want to get element number five, you write something like:
std::vector<int> data;
data[5] = 10;
But in your example you are using iterator. Think of iterator as a pointer to your Post
object. You cannot use that pointer as an index of element in your vector. So your code should look like this instead:
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 (currentPost->name.substr(0, baseName.length()) == baseName &&
utils::is_num(currentPost->name.substr(baseName.length(), std::string::npos)) &&
utils::to_int(currentPost->name.substr(baseName.length(), std::string::npos)) > postNum)
{
postNum = utils::to_int(currentPost->name.substr(baseName.length(), std::string::npos));
}
}
return baseName + utils::to_string(postNum);
}
Or you can use indexes, but then you cannot use iterators, for example:
std::string genBlankName (std::vector<Post> &posts)
{
std::string baseName = "New Post ";
int postNum = 1;
for (size_t currentPost = 0; currentPost < posts.size (); ++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);
}
Hope it helps. Happy coding!
Upvotes: 1
Reputation: 545923
Subscripting requires the use of an index, you’re using iterators.
You don’t need subscript at all, just dereference your iterator:
currentPost->name.substr(0, baseName.length())
… and so on.
Upvotes: 9