"is not a member of" 和 "no matching function for call to" 错误

"is not a member of" and "no matching function for call to" errors

我正在通过制作图 class(还有顶点和边 class)来学习 C++。我有很多错误:

Error   72  error C2039: 'other' : is not a member of '`global namespace''  c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 167
Error   92  error C2039: 'pointer' : is not a member of 'std::_Wrap_alloc<_Alloc>'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector   450
Error   98  error C2039: 'pointer' : is not a member of 'std::_Wrap_alloc<_Alloc>'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 104
Error   102 error C2039: 'pointer' : is not a member of 'std::_Wrap_alloc<_Alloc>'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector   458
Error   70  error C2039: 'rebind' : is not a member of '`global namespace'' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 167
Error   15  error C2039: 'type' : is not a member of 'std::_Get_pointer_type<_Ty>'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 421
Error   18  error C2039: 'type' : is not a member of 'std::_Get_pointer_type<_Ty>'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 248
Error   27  error C2039: 'type' : is not a member of 'std::_Get_pointer_type<_Ty>'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 255
Error   59  error C2146: syntax error : missing ';' before identifier 'allocate'    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 874
Error   84  error C2770: invalid explicit template argument(s) for '_Uty::void_pointer std::_Get_void_pointer_type<_Ty>::_Fn(int)'  c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 255

这些只是其中的一部分,还有很多(这些在 Visual Studio 2012 年)。我在代码块中也有一些(我正在尝试使用它们进行调试))。以下是一些代码块:

error: no matching function for call to 'make_heap(std::vector<std::pair<Vertex<int>*, double>, std::allocator<std::pair<Vertex<int>*, double> > >::iterator, std::vector<std::pair<Vertex<int>*, double>, std::allocator<std::pair<Vertex<int>*, double> > >::iterator, <unresolved overloaded function type>)'
note: candidates are:
note: template<class _RAIter> void std::make_heap(_RAIter, _RAIter)
note:   template argument deduction/substitution failed:
note:   candidate expects 2 arguments, 3 provided
note: void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std::pair<Vertex<int>*, double>*, std::vector<std::pair<Vertex<int>*, double>, std::allocator<std::pair<Vertex<int>*, double> > > >; _Compare = bool (Graph<int>::*)(std::pair<Vertex<int>*, double>, std::pair<Vertex<int>*, double>)]

以下是我的一些代码片段,可能指向错误的来源(我没有将所有代码都放在这里,因为我在“帮助”部分看到一个主题说我不应该放太多代码,只放什么需要):

template <typename T>
class Graph
{
private:
    std::vector< Vertex<T>* > m_vVertexes;
    int m_nIndexGenerator;

    Graph(const Graph& g);
    Graph& operator=(const Graph& g);
    void Init();
    bool Compare(std::pair< Vertex<T>*, double > pFrom, std::pair< Vertex<T>*, double > pTo);

template <typename T>
inline bool Graph<T>::Compare(std::pair< Vertex<T>*, double > pFrom, std::pair< Vertex<T>*, double > pTo)
{
    return pFrom.second >= pTo.second;
}

template <typename T>
bool Graph<T>::A_Star(int source, int destiny, double (* Heuristic)(const T& tFrom, const T& tTo), const bool testIfItsOnOpenSet)
{
    int vSize = m_vVertexes.size();
    bool found = false;

    // Checking if it is not out of the bounds
    if(source < 0 || destiny < 0 || source >= vSize || destiny >= vSize)
        return false;

    Vertex<T>* vDestiny = m_vVertexes[destiny];
    T& tDestinyInfo = vDestiny->GetInfo();

    // Initializing the vectors to proper values
    Init();
    m_vVertexes[source]->SetCost(0.0);
    std::pair< Vertex<T>*, double > pTempFrom;

    pTempFrom.second = Heuristic(m_vVertexes[source]->GetInfo(), tDestinyInfo);
    pTempFrom.first = m_vVertexes[source];

    // Initializing the minHeap
    std::vector< std::pair< Vertex<T>*, double > > minHeap;
    minHeap.reserve(vSize);

    std::make_heap(minHeap.begin(), minHeap.end(), Compare);
    minHeap.push_back(pTempFrom);
    std::push_heap(minHeap.begin(), minHeap.end(), Compare);

    while(!minHeap.empty())
    {
        std::pop_heap(minHeap.begin(), minHeap.end(), Compare);
        pTempFrom = minHeap.back();
        minHeap.pop_back();

        Vertex<T>* vTempFrom = pTempFrom.first;
        vTempFrom->SetColor(Black);

        double dCostFrom = vTempFrom->GetCost();

        if(vTempFrom == vDestiny)
        {
            found = true;
            break;
        }

        int fromIndex = vTempFrom->GetIndex();

        for(int i = vTempFrom->GetNextAdjacentVertex(); i > -1; i = vTempFrom->GetNextAdjacentVertex())
        {
            Vertex<T>* vTempTo = m_vVertexes[i];

            double dCost = dCostFrom + vTempFrom->GetEdgeCost(vTempTo->GetIndex);

            if(testIfItsOnOpenSet && vTempTo->GetColor() == Gray && dCost < vTempTo->GetCost())
            {
                vTempTo->SetColor(White);
                typename std::vector< Vertex<T>*, double >::iterator it = std::find(minHeap.begin(), minHeap.end(), vTempTo); // I only put this typename in there because CodeBlocks was complaining about it, I didn't get it though
                minHeap.erase(it);
                std::make_heap(minHeap.begin(), minHeap.end(), Compare);
            }

我没有 post 一切,我什至没有 post 整个 A_Star 算法,因为我认为我 post编辑

注意:所有代码都在 .h 文件中,因为如果我在 .cpp 文件中有定义,我将不得不说出我想要模板类型名的类型(至少我是这么理解的)。

@edit

这里是link,如果需要可以查看完整代码:https://github.com/Jordan2R/GraphDebug

  1. 您错过了 parens 此处:

    double dCost = dCostFrom + vTempFrom->GetEdgeCost(vTempTo->GetIndex());
                                                                       ^^^
    
  2. 你在这里打错了类型(还要注意我添加了 typename MSVC 错误地不需要):

    typename std::vector< Vertex<T>*, double >::iterator it = std::find(minHeap.begin(), minHeap.end(), vTempTo);
    

    double 指定了分配器预期导致您看到的大量错误的位置。将其修复为所需的类型,然后它就消失了:

    typename std::vector< std::pair<Vertex<T>*, double > >::iterator it = std::find(minHeap.begin(), minHeap.end(), vTempTo);
    
  3. 可悲的是,这暴露了一个新问题:您正在通过 "just a Vertex<T>* const" 在这些对的向量中进行搜索。那是行不通的,因为你无法比较它们。如果这确实是您想要的,请添加 operator== 重载,但实际上它看起来像您想要的

    std::map<Vertex<T>*, double> 
    

    而不是那个向量。我刚刚将其添加到进度编译中:

    // FIXING ERRORS IN "GUESS" MODE
    template <typename T>
    bool operator==(std::pair<Vertex<T>*, double> const& p, Vertex<T> const* v) {
        return p.first == v;
    }
    
  4. 最后,GetCost没有外部链接,所以你得到:

    || /tmp/cc36WPls.o: In function `Vertex<int>::GetEdgeCost(int)':
    Vertex.h|127| undefined reference to `Edge::GetCost()'
    

    从实施文件中删除inline

     /*not inline*/ double  Edge::GetCost() { return m_dCost; }
    

    Minor things:

  5. 无法在 Vertex& objects 上使用 -> 进行链接。使用 . 而不是 ->

  6. 在静态 class 成员函数上指定 static 在定义主体时是非法的 out-of-class

现在可以编译了。

我提出了拉取请求,以便更轻松地查看更改:https://github.com/Jordan2R/GraphDebug/pull/1

  • 1b23cd3 修复initialization/declaration顺序(我可以建议-Wall -pedantic
  • 6b29461 使 A_Star 中的 find 编译
  • ca09211 丢失 parens
  • ce7c6c1 小问题
  • ae349c4 不要滥用内联(它会弄乱外部链接)