Lecture 73 Compiler Error/Emplace Error

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    
    const FString WordListPath = FPaths::ProjectContentDir()/TEXT("Text File/HiddenWordList.txt");
    FFileHelper::LoadFileToStringArray(Words, *WordListPath);
    UBullCowCartridge::InitGame();
    /*for (int32 i = 0; i < 5; ++i)
    {
        PrintLine(TEXT("%s"), *Words[i]);
    }*/
    //PrintLine(TEXT("The hidden word is %s"), *HiddenWord);
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    ClearScreen();
    CheckGuess(Input);
}

void UBullCowCartridge::InitGame()
{
    ValidWords = GetValidWords(Words);
    HiddenWord = TEXT("machines");
    HWLength = HiddenWord.Len();
    Lives = HWLength;
    bGameOver = false;
    PrintLine(TEXT("The number of possible words is %i"), ValidWords.Num());
    PrintLine(TEXT("Moo!"));
    PrintLine(TEXT("Guess the %i letter word"), HWLength); //Remove hard coded number
    PrintLine(TEXT("Type in your guess and press enter\nLives: %i"), Lives);
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("\nPress Enter to continue"));
}

bool UBullCowCartridge::IsIsogram(const FString& Check) const
{
    for (int32 i = 0; i < InpLength; ++i)
    {
        for (int32 j = i + 1; j < InpLength; ++j)
        {
            if (Check[i] == Check[j])
            {
                return false;
            }
        }
    }
    return true;
}

void UBullCowCartridge::CheckGuess(const FString& Check)
{
    InpLength = Check.Len();

    if (bGameOver)
    {
        ClearScreen();
        InitGame();
        return;
    }
    
    if (Check == HiddenWord)
    {
        PrintLine(TEXT("You win!"));
        EndGame();
        return;
    }

    if (InpLength != HWLength)
    {
        PrintLine(TEXT("Incorrect guess, try again.\nLives: %i"), Lives);
        PrintLine(TEXT("The hidden word is %i characters long"), HWLength);
        return;
    }
    
    if (!IsIsogram(Check))
    {
        PrintLine(TEXT("No repeating letters! Try again!"));
        return;
    }

    --Lives;
    PrintLine(TEXT("You've lost a life!\nLives: %i"), Lives);

    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left"));
        PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
        
        EndGame();
        return;
    }
}

TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> Starter) const
{
    for (int i = 0; i < Starter.Num(); ++i)
    {
        if (Starter[i].Len() >= 4 && Starter[i].Len() <= 8)
        {
            ValidWords.Emplace(Starter[i]);
        }
    }
    return ValidWords;
}

The error: ‘int TArray<FString,FDefaultAllocator>::Emplace<FString&>(FString &)’: cannot convert ‘this’ pointer from ‘const TArray<FString,FDefaultAllocator>’ to ‘TArray<FString,FDefaultAllocator> &’

I’m not see where I’m allegedly using a pointer.

So I found the fix. I don’t know why this fixed it, but it did: originally I was declaring the ValidWords array in the header file, and then updating it with the function we were creating. I tried removing the const keyword and that didn’t seem to change anything, just gave a different type of error. So instead of doing that, I got rid of it and tried doing it exactly as shown in the video where the ValidWords array is declared in the function. If anyone has any clue as to why this works, I would appreciate it. Otherwise, this is solved.

It’s due to the const, you probably didn’t remove it from both declaration and definition.

A const member function can’t modify data members but you’re doing that within that function by calling Emplace on ValidWords.

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

Privacy & Terms