簡體   English   中英

C ++ xstddef編譯器錯誤

[英]c++ xstddef compiler errors

為了學習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

錯誤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

錯誤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

錯誤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

錯誤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

錯誤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

錯誤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

錯誤7錯誤C2676:二進制'<':'const LocationNode'未定義此運算符或未轉換為預定義運算符可接受的類型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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM