0

So I'm trying to code a GOAP AI behavior in C++. To do so I have to program a AStar Pathfinding algorithm and doing so i ran into this error:

Error   C2678   binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)    SDL_Decisions

Showing in the compiler:

        1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xstddef(141): error C2678: binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)
1>        with
1>        [
1>            _Ty=State
1>        ]
1>c:\users\computer\desktop\tercero_enti_ia\aa4\src\utils.h(75): note: could be 'const bool State::operator <(const State &)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\system_error(319): note: or       'bool std::operator <(const std::error_condition &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\system_error(312): note: or       'bool std::operator <(const std::error_code &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xstddef(141): note: while trying to match the argument list '(const _Ty, const _Ty)'
1>        with
1>        [
1>            _Ty=State
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xstddef(140): note: while compiling class template member function 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const'
1>        with
1>        [
1>            _Kty=State,
1>            _Ty=State
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(1110): note: see reference to function template instantiation 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const' being compiled
1>        with
1>        [
1>            _Kty=State,
1>            _Ty=State
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(264): note: see reference to class template instantiation 'std::less<_Kty>' being compiled
1>        with
1>        [
1>            _Kty=State
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(264): note: see reference to variable template 'const bool is_empty_v<std::less<State> >' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree(1032): note: see reference to class template instantiation 'std::_Tree_comp_alloc<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<State,int,std::less<State>,std::allocator<std::pair<const State,int>>,false>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map(82): note: see reference to class template instantiation 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>' being compiled
1>        with
1>        [
1>            _Kty=State,
1>            _Ty=int,
1>            _Pr=std::less<State>,
1>            _Alloc=std::allocator<std::pair<const State,int>>
1>        ]
1>c:\users\computer\desktop\tercero_enti_ia\aa4\src\goap_astar.cpp(19): note: see reference to class template instantiation 'std::map<State,int,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
1>        with
1>        [
1>            _Kty=State,
1>            _Ty=int
1>        ]

and my code looks like this:

    void GOAP_AStar::run(Agent * agent, float dtime)
    {
        State initState = agent->bbData->GetState(bbVariables::STARTING_STATE);
        State targetState = agent->bbData->GetState(bbVariables::OBJECTIVE);
        std::map <State, int> costSoFar;
        std::map <State, Actions> camefrom;
        // Add the first node to frontier.
        frontier.push(GOAPComparer(Actions("init",initState,initState), 0.0f));

        costSoFar[(State)initState] = 0;
        ...
    }

the line that makes the error come up is this one:

costSoFar[(State)initState] = 0;

And my struct State looks like this:

struct State
{
    int alive;
    int have_gun;
    int gun_reloaded;
    int has_bomb;
    int enemy_visible;
    int enemy_aligned;
    int enemy_near;
    int enemy_alive;

    State()
    {

    }
    State(int _alive, int _havegun, int _gunreloaded, int _hasbomb, int _enemyVisible, int _enemyAligned, int _enemyNear, int _enemyAlive)
    {
        alive = _alive;
        have_gun = _havegun;
        gun_reloaded = _gunreloaded;
        has_bomb = _hasbomb;
        enemy_visible = _enemyVisible;
        enemy_aligned = _enemyAligned;
        enemy_near = _enemyNear;
        enemy_alive = _enemyAlive;
    }
    inline void operator=(const State& v2)
    {
        alive = v2.alive;
        have_gun = v2.have_gun;
        gun_reloaded = v2.gun_reloaded;
        has_bomb = v2.has_bomb;
        enemy_visible = v2.enemy_visible;
        enemy_aligned = v2.enemy_aligned;
        enemy_near = v2.enemy_near;
        enemy_alive = v2.enemy_alive;
    }

    inline bool operator==(const State& v2)
    {
        return  (alive == v2.alive || v2.alive == 2) &&
            (have_gun == v2.have_gun || v2.have_gun == 2) &&
            (gun_reloaded == v2.gun_reloaded || v2.gun_reloaded == 2) &&
            (has_bomb == v2.has_bomb || v2.has_bomb == 2) &&
            (enemy_visible == v2.enemy_visible || v2.enemy_visible == 2) &&
            (enemy_aligned == v2.enemy_aligned || v2.enemy_aligned == 2) &&
            (enemy_near == v2.enemy_near || v2.enemy_near == 2) &&
            (enemy_alive == v2.enemy_alive || v2.enemy_alive == 2);
    }
    inline bool operator!=(const State& v2)
    {
        return  !(*this == v2);
    }

    const bool operator <( const State& v2) 
    {
    return 
    std::tie(this->alive, this->enemy_aligned, this->enemy_alive, this->enemy_near, this->enemy_visible, this->gun_reloaded, this->has_bomb, this->have_gun) 
    < 
    std::tie(v2.alive, v2.enemy_aligned, v2.enemy_alive, v2.enemy_near, v2.enemy_visible, v2.gun_reloaded, v2.has_bomb, v2.have_gun);
}
};

Thanks in advance for any help you can provide

user12656119
  • 73
  • 1
  • 7
  • 1
    That's only part of the error message. There's more just before it. – molbdnilo Jan 07 '20 at 09:23
  • 2
    the third template param for std::map is a comparator. see https://en.cppreference.com/w/cpp/container/map – Alexander Jan 07 '20 at 09:25
  • 1
    What is the purpose of `State` being the third template argument of your `map`s? You can omit this argument since you define `bool operator<` for `State`. Just make this `bool operator<` a _constant member function_. – Daniel Langr Jan 07 '20 at 09:26
  • are you sure that you need std::map and not std::unordered_map (aka hash map)? – Alexander Jan 07 '20 at 09:27
  • 1
    you have operator < in your state it means you do not need a third param in the map at all. BTW please mark your < operator as const. – Alexander Jan 07 '20 at 09:30
  • 1
    You can omit the third template argument of `map`s since you define `operator<` for `State`. Just make this `operator<` a _constant member function_. However, your definition of `operator<` is wrong, since it does not define order. For example, for objects `a` and `b` that have the same value of any member variable, both `a – Daniel Langr Jan 07 '20 at 09:34
  • 1
    See this question how to define a correct _less-than_ operator for structs: [Defining operator< for a struct](https://stackoverflow.com/q/3882467/580083). – Daniel Langr Jan 07 '20 at 09:42
  • hi @DanielsaysreinstateMonica so i have done what you told me, the error changed but the compiler output didn't so much, added the error output to the question – user12656119 Jan 07 '20 at 09:49
  • 2
    @user12656119 You did not make your less-than operator a constant member function. Change `const bool operator <( const State& v2)` to `bool operator <( const State& v2) const`. – Daniel Langr Jan 07 '20 at 09:54
  • oh right, sorry. That solved the issue thank you so much – user12656119 Jan 07 '20 at 10:01

0 Answers0