簡體   English   中英

C ++中向量的奇怪錯誤

[英]Strange Error with Vectors in C++

每次我為我正在處理的RTS項目編譯代碼時,都會出現此錯誤:

error C2664: 'std::_Vector_iterator<_Ty,_Alloc> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Ty,_Alloc>)' : cannot convert parameter 1 from 'MAPTILE *' to 'std::_Vector_const_iterator<_Ty,_Alloc>'

我對此方法的代碼區域是這樣的:

MAPTILE *startTile = GetTile(start);
    MAPTILE *goalTile = GetTile(goal);

...

    std::vector<MAPTILE*> open;             //Create Our Open list
    startTile->g = 0;                       //Init our starting point (SP)
    startTile->f = H(start, goal);
    startTile->open = true;
    open.push_back(startTile);              //Add SP to the Open list

    bool found = false;                  // Search as long as a path hasnt been found,
    while(!found && !open.empty())       // or there is no more tiles to search
    {                                               
        MAPTILE * best = open[0];        // Find the best tile (i.e. the lowest F value)
        int bestPlace = 0;
        for(int i=1;i<open.size();i++)
            if(open[i]->f < best->f)
            {
                best = open[i];
                bestPlace = i;
            }

        if(best == NULL)break;          //No path found

        open[bestPlace]->open = false;
        open.erase(&open[bestPlace]);   // Take the best node out of the Open list

我已經在該網站和其他一些網站上進行了搜索,但是找不到正確的區域。

我正在閱讀Carl Granberg撰寫的“使用Direct3D編程RTS游戲”。

我所有的代碼都是正確的,因為我將其與源代碼匹配,並且遇到了相同的錯誤。

我正在使用Visual C ++ 2008 Express Edition。

我從來沒有遇到過這個錯誤。

erase一個迭代器,並且您為其賦予了指向數組中元素的指針。 使用std::advance或保留一個迭代器的句柄。

std::vector<MAPTILE*> p = open.begin();
std::advance(p, bestPlace);
open.erase(p);

編輯 我忘記了std::advance不返回任何東西。 對於那個很抱歉。 大約五年來我都沒有認真進行過C ++開發。

我會重寫您的for循環以使用迭代器而不是索引。

我相信以下代碼段等同於用迭代器替換數組索引后的代碼段。 我必須添加typedef使其更具可讀性。

    MAPTILE *startTile = GetTile(0);
    startTile->g = 0;
    startTile->f = H(0, 10);
    startTile->open = true;

    std::vector<MAPTILE*> open;
    open.push_back(startTile);

    typedef std::vector<MAPTILE*>::iterator Iterator;
    bool found = false;
    while (!found && !open.empty()) {
            Iterator best = open.begin();
            for (Iterator iter=best+1, end=open.end();
                 iter!=end; ++iter)
            {
                    if ((*iter)->f < (*best)->f) {
                            best = iter;
                    }
            }
            if (*best == NULL) {
                    break;
            }
            (*best)->open = false;
            open.erase(best);
    }

我不完全相信if (*best == NULL)條件是否會被匹配。 這在clang++下編譯沒有錯誤(對不起,我家中沒有Visual Studio)。 當我通過clang++運行您的代碼片段時,我得到的故障與您的類似:

foo.cpp:85:14: error: calling a private constructor of class
      'std::__1::__wrap_iter<MAPTILE *const *>'
                open.erase(&open[bestPlace]);
                           ^
    /Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1381:31: note:
      declared private here
    _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT_D...
                              ^
1 error generated.

我不知道還有什么要說的, std::vector<>::erase帶有std::vector<>::iterator參數,僅此而已。 我的猜測是,這本書是針對VC ++版本編寫的,該版本使用了用於向量迭代器的指針,盡管我不記得曾經這樣做過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM