我正在通过制作Graph类(以及Vertex和Edge类)来学习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);
}
我没有发布所有内容,甚至没有发布整个A_Star算法,因为我认为错误出在我发布的内容中。
注意:所有这些代码都在.h文件中,因为如果我在.cpp文件中有定义,则必须说出我要为模板类型名使用哪种类型(至少这是我的理解)。
@编辑
如果需要,这里是查看完整代码的链接:https : //github.com/Jordan2R/GraphDebug
您在这里错过了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
在A_Star中进行编译本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句