简体   繁体   English

C ++ xstddef编译器错误

[英]c++ xstddef compiler errors

To learn c++ I tried to implement an algorithem in visual studio using classes and more. 为了学习c ++,我尝试使用类等在Visual Studio中实现算法。 After finishing the code and a bit of debugging I got weird complier errors that I dont understand. 完成代码并进行一些调试之后,我得到了我不理解的奇怪的编译器错误。 Can someone help identify the problam? 有人可以帮助您识别问题吗?

(More detail: visual studio 2012) (更多详细信息: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;
}

Here are the compiler errors: ( https://i.imgur.com/sPcoZwm.png ) 以下是编译器错误:( 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 错误1错误C2784:'bool std :: operator <(const std :: list <_Ty,_Alloc>&,const std :: list <_Ty,_Alloc>&)':无法推断'const std ::的模板参数清单<_Ty,_Alloc>&'from'const LocationNode'c:\\程序文件\\ 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 错误2错误C2784:'bool std :: operator <(const std :: _ Tree <_Traits>&,const std :: _ Tree <_Traits>&)':无法推断'const std :: _ Tree <_Traits>的模板参数&'从'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 错误3错误C2784:'bool std :: operator <(const std :: move_iterator <_RanIt>&,const std :: move_iterator <_RanIt2>&)':无法推断'const std :: move_iterator <_RanIt>的模板参数&'从'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 错误4错误C2784:'bool std :: operator <(const std :: reverse_iterator <_RanIt>&,const std :: reverse_iterator <_RanIt2>&)':无法推导'const std :: reverse_iterator <_RanIt>的模板参数&'从'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 错误5错误C2784:'bool std :: operator <(const std :: _ Revranit <_RanIt,_Base>&,const std :: _ Revranit <_RanIt2,_Base2>&)':无法推断'const std ::的模板参数_Revranit <_RanIt,_Base>&'from'const LocationNode'c:\\程序文件\\ 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 错误6错误C2784:'布尔std :: operator <(const std :: pair <_Ty1,_Ty2>&,const std :: pair <_Ty1,_Ty2>&)':无法推断'const std ::的模板参数对<_Ty1,_Ty2>和'来自'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 错误7错误C2676:二进制'<':'const LocationNode'未定义此运算符或未转换为预定义运算符可接受的类型c:\\ program files \\ microsoft visual studio 11.0 \\ vc \\ include \\ xstddef 180 1 ConsoleApplication45

Thanks! 谢谢!

Use of 用于

    nodes.sort(my_compare);

is a problem since my_compare uses arguments of type LocationNode while the elements of nodes are of type LocationNode* . 这是一个问题,因为my_compare使用LocationNode类型的参数,而nodes的元素是LocationNode*类型。 Change my_compare to: my_compare更改为:

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

The other error is that the < operator is not defined between two objects of type LocationNode . 另一个错误是<运算符未在LocationNode类型的两个对象之间定义。 It is necessary for you to be able to use: 您必须能够使用:

map<LocationNode,int> neighbors;

Add the following member function in LocationNode to resolve that error. LocationNode添加以下成员函数可解决该错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM