2011-02-11 41 views
2

的代碼我試圖運行:不明白爲什麼我沒有被允許下標這一載體

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> > >&)

對不起,不說更多,但我認爲這是一個很普遍的事情,我只是不知道作爲一個結局。我會谷歌它,但它似乎過於普遍的問題,使任何有用的,因爲我懷疑這是更多的問題與我的實施或沿着這些線路。

回答

9

下標需要使用索引,您正在使用迭代器。

你根本不需要下標,只是解引用您的迭代器:

currentPost->name.substr(0, baseName.length()) 

...等等。

+0

Aww該死的,17秒! – Xeo 2011-02-11 14:23:57

+0

@Xeo:提高你的打字技巧:-D – 2011-02-11 14:34:54

6

您不要在下標上使用迭代器。只要看看size_t

for (size_t currentPost = 0; currentPost < posts.size(); ++currentPost) 

或者間接引用迭代器:

currentPost->name.substr(0, baseName.length()) 
1

std::vector<typename T>是一個隨機存取容器,但是,你必須使用偏移訪問在給定位置的元素。例如,如果你想得到五號元素,你可以寫下類似的東西:

std::vector<int> data; 
data[5] = 10; 

但是在你的例子中你使用的是迭代器。把迭代器想象成指向你的對象的指針。您不能將該指針用作向量中元素的索引。所以,你的代碼看起來應該是這樣,而不是:

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); 
} 

或者你也可以使用索引,但你不能使用迭代器,例如:

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); 
} 

希望它能幫助。快樂的編碼!

相關問題