gpt4 book ai didi

C++ xstddef 编译器错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:30 25 4
gpt4 key购买 nike

为了学习 C++,我尝试使用类等在 Visual Studio 中实现一个算法。完成代码和一些调试后,我遇到了我不明白的奇怪的编译器错误。有人可以帮助确定问题所在吗?

(更多细节:visual studio 2012)

    // ConsoleApplication45.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>
#include <map>
#include <list>
#include <math.h>
using namespace std;

class LocationNode;
class NodeMap;


class LocationNode
{
private:
char name;
int xLocation;
int yLocation;
map<LocationNode,int> neighbors;
LocationNode *previous;
int score;

int CalcDistance(LocationNode &dest)
{
return (int)sqrt(pow(dest.xLocation-xLocation,2) + pow(dest.yLocation-yLocation,2));
}

public:
int finalScore;
LocationNode(char name, int x, int y)
{

this->name = name;
this->xLocation = x;
this->yLocation = y;
}

string GetPath()
{

return string(1, name).append((*previous).GetPath());
}

void Connect(LocationNode &other, int weight)
{
this->neighbors.insert(std::make_pair(other,weight));
}

void CalcScore(LocationNode &previous, LocationNode &dest)
{
score = previous.score + neighbors[previous];
finalScore = previous.score + neighbors[previous] + CalcDistance(dest);
}
void CalcNeighbors(LocationNode &dest)
{
for (pair<LocationNode,int> node : neighbors)
{
node.first.CalcScore(*this,dest);
}
}

};

bool my_compare (LocationNode a, LocationNode b)
{
return a.finalScore < b.finalScore;
}




class NodeMap
{
private:
static LocationNode &str;
static LocationNode &dest;
static LocationNode *node;
static list<LocationNode*> nodes;
static void loop(bool isFirst)
{
if(isFirst)
{
node = &str;
}

(*node).CalcNeighbors(dest);
nodes.sort(my_compare);
node = nodes.front();
}
public:
static string start()
{
Init();
loop(true);
while(node != &dest)
{
loop(false);
}
return dest.GetPath();
}
static void Init()
{
LocationNode A = *(new LocationNode('A',1,2));
nodes.push_back(&A);
LocationNode B = *(new LocationNode('B',7,1));
nodes.push_back(&B);
LocationNode C = *(new LocationNode('C',2,8));
nodes.push_back(&C);
LocationNode D = *(new LocationNode('D',4,3));
nodes.push_back(&D);
LocationNode E = *(new LocationNode('E',9,6));
nodes.push_back(&E);
LocationNode F = *(new LocationNode('F',1,2));
nodes.push_back(&F);
A.Connect(B,2);
B.Connect(D,3);
D.Connect(E,2);
E.Connect(F,3);
A.Connect(C,1);
C.Connect(F,10);
dest = F;
str = A;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
cout << &(NodeMap::start());
cin.get();
return 0;
}

以下是编译错误:( https://i.imgur.com/sPcoZwm.png )

Error 1 error C2784: 'bool std::operator <(const std::list<_Ty,_Alloc> &,const std::list<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::list<_Ty,_Alloc> &' from 'const LocationNode' c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const LocationNode' c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

Error 3 error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const LocationNode' c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

Error 4 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const LocationNode' c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

Error 5 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const LocationNode' c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

Error 6 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const LocationNode' c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

Error 7 error C2676: binary '<' : 'const LocationNode' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 11.0\vc\include\xstddef 180 1 ConsoleApplication45

谢谢!

最佳答案

使用

    nodes.sort(my_compare);

是一个问题,因为my_compare使用 LocationNode 类型的参数而 nodes 的元素类型为 LocationNode* .更改 my_compare到:

bool my_compare (LocationNode* a, LocationNode* b)
{
return a->finalScore < b->finalScore;
}

另一个错误是 < LocationNode 类型的两个对象之间未定义运算符.您必须能够使用:

map<LocationNode,int> neighbors;

LocationNode中加入如下成员函数解决该错误。

bool operator<(LocationNode const& rhs) const
{
// Use whatever makes sense for your application.
return (finalScore < rhs.finalScore);
}

关于C++ xstddef 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48861227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com