Download

Help! How do I sort this? TArray<TSharedPtr<UMyClass>>

Anybody know how to Sort a **TArray **of TSharedPtr<T>? I end up with this error


C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Templates/Sorting.h(23): error C2664: 'bool ULevelGenTask::FindPath::<lambda_1d49b448e8cff49693ac1b954b1bc40d>::operator ()(UTileInfo &,UTileInfo &) const': cannot convert argument 1 from 'T' to 'UTileInfo &'

1> with

1> 

1> T=TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>

1> ]



Here is my code


TArray<TSharedPtr<UTileInfo>> open;

// sort by f score to target room

open.Sort(&](UTileInfo &a, UTileInfo &b) {

return a.f > b.f;

});

Your lambda params are wrong. You have an array of TSharedPtr<UTileInfo> not of UTileInfo. Change it to this, update the rest of the code to match, and it should fix the compile error.


open.Sort(&](TSharedPtr<UTileInfo> &a, TSharedPtr<UTileInfo> &b)

Thank you so much for the quick reply and suggestions! I tried that previously and still ran into issues. I’ve updated my code to the following, and please see the build output. I’m not really sure what’s going on here, but I feel like I’m close!


TArray<TSharedPtr<UTileInfo>> open;
// sort by f score to target room
open.Sort(&](TSharedPtr<UTileInfo>& a, TSharedPtr<UTileInfo>& b) {
return a->f > b->f;
});


Rebuild started...
1>------ Rebuild All started: Project: rts, Configuration: DebugGame_Editor x64 ------
2>------ Skipped Rebuild All: Project: UE4, Configuration: BuiltWithUnrealBuildTool Win32 ------
2>Project not selected to build for this solution configuration
1>Cleaning rtsEditor and UnrealHeaderTool binaries...
1>Using 'git status' to determine working set for adaptive non-unity build (C:\repo\rts).
1>Creating makefile for rtsEditor (no existing makefile)
1>Parsing headers for rtsEditor
1> Running UnrealHeaderTool "C:\repo\rts\rts\rts.uproject" "C:\repo\rts\rts\Intermediate\Build\Win64\rtsEditor\DebugGame\rtsEditor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -abslog="C:\Users\gvart\AppData\Local\UnrealBuildTool\Log_UHT.txt" -installed
1>Reflection code generated for rtsEditor in 7.3790333 seconds
1>Building rtsEditor...
1>Using Visual Studio 2019 14.28.29335 toolchain (C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333) and Windows 10.0.18362.0 SDK (C:\Program Files (x86)\Windows Kits\10).
1>Building 14 actions with 24 processes...
1> [1/14] Default.rc2
1> [2/14] SharedPCH.Engine.NonOptimized.ShadowErrors.cpp
1> [3/14] rts.cpp
1> [4/14] QuadTree.cpp
1> [5/14] rts.init.gen.cpp
1> [6/14] Graph.cpp
1> [7/14] RTSFunctionLibrary.gen.cpp
1> [8/14] RTSFunctionLibrary.cpp
1> [9/14] LevelGenerator.cpp
1> [10/14] LevelGenerator.gen.cpp
1> [11/14] LevelGenTask.cpp
1>C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Templates/Sorting.h(24): error C2664: 'bool ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>::operator ()(TSharedPtr<UTileInfo,ESPMode::NotThreadSafe> &,TSharedPtr<UTileInfo,ESPMode::NotThreadSafe> &) const': cannot convert argument 1 from 'const T' to 'TSharedPtr<UTileInfo,ESPMode::NotThreadSafe> &'
1> with
1> 
1> T=TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>
1> ]
1> C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Templates/Sorting.h(24): note: Conversion loses qualifiers
1> C:\repo\rts\rts\Source\rts\Utility\LevelGenTask.cpp(262): note: see declaration of 'ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>::operator ()'
1> C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Templates/Sorting.h(24): note: while compiling class template member function 'bool TDereferenceWrapper<T,PREDICATE_CLASS>::operator ()(const T &,const T &) const'
1> with
1> 
1> T=TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>,
1> PREDICATE_CLASS=ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>
1> ]
1> C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Templates/Invoke.h(51): note: see reference to function template instantiation 'bool TDereferenceWrapper<T,PREDICATE_CLASS>::operator ()(const T &,const T &) const' being compiled
1> with
1> 
1> T=TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>,
1> PREDICATE_CLASS=ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>
1> ]
1> C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Templates/Sorting.h(80): note: see reference to class template instantiation 'TDereferenceWrapper<T,PREDICATE_CLASS>' being compiled
1> with
1> 
1> T=TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>,
1> PREDICATE_CLASS=ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>
1> ]
1> C:\software\UE_4.26\Engine\Source\Runtime\Core\Public\Containers/Array.h(2506): note: see reference to function template instantiation 'void Sort<TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>,PREDICATE_CLASS>(T *,const int32,const PREDICATE_CLASS &)' being compiled
1> with
1> 
1> PREDICATE_CLASS=ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>,
1> T=TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>
1> ]
1> C:\repo\rts\rts\Source\rts\Utility\LevelGenTask.cpp(262): note: see reference to function template instantiation 'void TArray<TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>,FDefaultAllocator>::Sort<ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>>(const PREDICATE_CLASS &)' being compiled
1> with
1> 
1> PREDICATE_CLASS=ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>
1> ]
1> C:\repo\rts\rts\Source\rts\Utility\LevelGenTask.cpp(260): note: see reference to function template instantiation 'void TArray<TSharedPtr<UTileInfo,ESPMode::NotThreadSafe>,FDefaultAllocator>::Sort<ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>>(const PREDICATE_CLASS &)' being compiled
1> with
1> 
1> PREDICATE_CLASS=ULevelGenTask::FindPath::<lambda_6a01d443e2166dadc9471417dfaf25a5>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.MakeFile.Targets(54,5): error MSB3073: The command "C:\software\UE_4.26\Engine\Build\BatchFiles\Rebuild.bat rtsEditor Win64 DebugGame -Project="C:\repo\rts\rts\rts.uproject" -WaitMutex -FromMsBuild" exited with code -1.
1>Done building project "rts.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 1 skipped ==========

Make the lambda params const.

I think that fixed it! thank you so much

ddbrown30 you are the true G.
If I ever finish this game you get a shout-out.

Hah. No problem. :slight_smile: