IsIsogram Function Error -- Why Doesn't This Work?

Hi all.

I previously worked through the old Bull Cow game where Ben did it in Visual Studio rather than creating a UE4 project around it. I leaned on my old code (probably Ben’s old code) where I used a map rather than the nested for loops and tried to implement it in this project, where I got a nullptr error. Does anyone have any idea why this code would be throwing an error?

First of all, the old code used a line to reference maps as TMaps. I imagine this may be part of the issue, but I thought they worked the same in UE4.
#define TMap std::map

The code I tried to implement that threw the error was this:

bool UBullCowCartridge::IsIsogram(const FString& Guess) const
{
    if (Guess.IsEmpty()) // Line 146
    {
        PrintLine("Oops. Something went wrong... try again!");
        return false;
    }

    TMap<char, bool> LettersSeen = TMap<char, bool>(); // Line 152

    for (int32 index = 0; index < Guess.Len(); index++) // Line 154
    {
        if (LettersSeen[Guess[index]]) // Line 156 <-- This is where the nullptr is being thrown
        {
            // Duplicate letter
            return false;
        }
        else
        {
            LettersSeen[Guess[index]] = true;
        }
    }

    return true; // Line 167
}

The crash reporter is giving me the following traceback:

Assertion failed: Pair != nullptr [File:C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Containers/Map.h] [Line: 621]

UE4Editor_Core
UE4Editor_Core
UE4Editor_BullCowGame_1722!UBullCowCartridge::IsIsogram() [C:\Users\Casey\Documents\GitHub\Bulls-And-Cows-Game\Source\BullCowGame\BullCowCartridge.cpp:156]
UE4Editor_BullCowGame_1722!UBullCowCartridge::OnInput() [C:\Users\Casey\Documents\GitHub\Bulls-And-Cows-Game\Source\BullCowGame\BullCowCartridge.cpp:83]
UE4Editor_BullCowGame_1722!UTerminal::AcceptInputLine() [C:\Users\Casey\Documents\GitHub\Bulls-And-Cows-Game\Source\BullCowGame\Console\Terminal.cpp:143]
UE4Editor_BullCowGame_1722!UTerminal::OnKeyDown() [C:\Users\Casey\Documents\GitHub\Bulls-And-Cows-Game\Source\BullCowGame\Console\Terminal.cpp:112]
UE4Editor_BullCowGame_1722!TBaseUObjectMethodDelegateInstance<0,UTerminal,void __cdecl(FKey),FDefaultDelegateUserPolicy>::Execute() [C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:593]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Any help on this issue is greatly appreciated!

EDIT 1: Added line numbers in various parts of the code blurb.

Hi all.

I’m a dummy. I figured out that TMaps in UE4 do not work exactly like maps in C++ in that they must have the value in the map before using it. I knew other languages had this rule but I learned a while back that C++ had allowed some funky voodoo access-and-initialize operation in one go. UE4 is not as lenient apparently.

Here’s the updated (WORKING) code for anyone who is curious:


bool UBullCowCartridge::IsIsogram(const FString& Guess) const
{
    if (Guess.IsEmpty())
    {
        PrintLine("Oops. Something went wrong... try again!");
        return false;
    }

    TMap<char, bool> LettersSeen = TMap<char, bool>();

    for (int32 index = 0; index < Guess.Len(); index++)
    {
        if (LettersSeen.Contains(Guess[index])) // Check if the key is in the map
        {
            // Key is in the map

            if (LettersSeen[Guess[index]]) // Check if the value at that key is true
            {
                // Duplicate letter
                return false;
            }
            else // Value is false
            {
                // Set to true to indicate letter is in the word
                LettersSeen[Guess[index]] = true;
            }
        }
        else // Letter is NOT in the map
        {
            // Add the letter to the map
            LettersSeen.Add(Guess[index], true);
        }
    }

    return true;
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms