简体   繁体   中英

c++ xstddef compiler errors

To learn c++ I tried to implement an algorithem in visual studio using classes and more. 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)

    // 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 )

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

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* . Change my_compare to:

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 . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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