Need help with adding an element to a TArray filled with pointers

First of all I have to say that I feel very embarassed for having to post such a noobish question but I’ve been trying for the better part of the last 3 hours to no avail and my deadlines cannot wait.

I have the following code



UGoapAction* ac1 = NewObject<UGoapAction>(); ac1->Name = "A1";
UGoapAction* ac2 = NewObject<UGoapAction>(); ac2->Name = "A2";
UGoapAction* ac3 = NewObject<UGoapAction>(); ac3->Name = "A3";

UHumbleAStarNode* a1 = NewObject<UHumbleAStarNode>(); a1->Initialize(ac1); a1->HCost = 5;
UHumbleAStarNode* a2 = NewObject<UHumbleAStarNode>(); a2->Initialize(ac2); a2->HCost = 1;
UHumbleAStarNode* a3 = NewObject<UHumbleAStarNode>(); a3->Initialize(ac3); a3->HCost = 3;

UHumbleAStarNode const* ca1 = a1;
UHumbleAStarNode const* ca2 = a2;
UHumbleAStarNode const* ca3 = a3;

TArray<UHumbleAStarNode const *> t;
t.Add(ca1); //I've tried with a1 here as well - same error message
t.Add(ca2);
t.Add(ca3);


As you can see I have a TArray which hold elements of type


UHumbleAStarNode const *

so pointers to constant objects.

I’ve defined a “<” operator for this class so the Heapify methods of the TArray can use it.
It’s defined as


bool operator < (UHumbleAStarNode const & rhs) const;

And when I try to Add ca1, ca2, ca3(or a1, a2, a3) to the array I get this error compilation message:



2>D:\Epic Games\UE_4.23\Engine\Source\Runtime\Core\Public\Templates/Sorting.h(38): error C2664: 'bool TLess<UHumbleAStarNode *>::operator ()(const T &,const T &) const': cannot convert argument 1 from 'T' to 'const T &'
2>          with
2>          
2>              T=UHumbleAStarNode *
2>          ]
2>          and
2>          
2>              T=UHumbleAStarNode
2>          ]
2>          and
2>          
2>              T=UHumbleAStarNode *
2>          ]


I’ve tried all variations of pointers and references and constants and I can’t get it to work.

https://forums.unrealengine.com/deve…-exposed-to-bp

This thread mentions that this could happen if I were calling the Add function in a const function, which is not the case.

Any help would be greatly appreciated!

Edit: Okay I wen away for a couple of hours and when I came back and tried recompiling it finally compiled lol. However, I need to use the TArray<UHumbleAStarNode const *> as a priority queue so I tried calling


TArray::Heapify()

on it and unfortunately I’m getting the exact same error.

Okay, so I managed to make to compile the project Heapify() work. I removed the “<” operator from my UHumbleAStarNode class and started using the Heap overload methods which accept predicate as parameter.

Now my code looks like this


    TArray<UHumbleAStarNode const *> t;
    t.Add(a1);
    t.Add(a2);
    t.Add(a3);
    t.Add(a4);
    t.Add(a5);
    t.Add(a6);

    auto pred = ](UHumbleAStarNode const& lhs, UHumbleAStarNode const& rhs)
    {
        return lhs.GetFValue() < rhs.GetFValue();
    };

    t.Heapify(pred);

and it works a-ok. However I am still very interested in why I couldn’t compile with the “<” operator defined in my class.

You need to define operator as a free(often friend) function with two-arguments (exactly what you do in predicate-version)



class foo
{
  int value;
  friend operator<(foo const& lhs, foo const& rhs) { return lhs.value < rhs.value; }
};

Okay I added this function to the UHumbleAStarNode class


    friend bool operator < (UHumbleAStarNode const& lhs, UHumbleAStarNode const& rhs)
    {
        return lhs.GetFValue() < lhs.GetFValue();
    }

and it’s still giving me the same error