我正在通过创建一个Graph类来学习C ++(同时使用Vertex和Edge类)。我有很多错误:
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);
}
我没有发布所有内容,我甚至没有发布整个A_Star算法,因为我认为错误在我发布的地方。
注意:所有代码都在.h文件中,因为如果我在.cpp文件中有定义,我将不得不说出我想要的模板类型名称(至少这是我所理解的)。
@edit
以下是需要时查看整个代码的链接:https://github.com/Jordan2R/GraphDebug
答案 0 :(得分:4)
你错过了parens:
double dCost = dCostFrom + vTempFrom->GetEdgeCost(vTempTo->GetIndex());
^^^
您在此处输错了类型(请注意,我添加了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);
遗憾的是,这会产生一个新问题:您正在通过“只是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;
}
最后,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; }
小事:
无法在->
个对象上与Vertex&
进行链接。使用.
代替->
在定义正文中的正文时,在静态类成员函数上指定static
是非法的
现在编译。
我已提出拉取请求,因此您可以更轻松地查看更改:https://github.com/Jordan2R/GraphDebug/pull/1
-Wall -pedantic
)find