"member function may not be declared outside of it's class" Error

Recently i have come across a problem in my “BullCowCartrage.cpp” file, an error stating “member function ‘void UBullCowCartridge::InitGame()’ may not be declared outside of it’s class”

my code looks like the following:

void UBullCowCartridge::InitGame();

{

// welcoming the player

PrintLine(TEXT("Welcome to Ezra's bullcow game!"));

PrintLine(TEXT("guess the %i letter word."), HiddenWord.Len());

PrintLine(TEXT("type in your guess and press enter to"));

PrintLine(TEXT("continue...")); // prompt player for guess

HiddenWord = TEXT("cakes");

Lives = 5;

bGameOver = false;

}

does anyone actually know what the problem is here?

Your semicolon here makes this a declaration and you can’t declare member functions outside of the class definition.

class Example
{
    void Foo(); // member function declaration inside class
};

void Example::Foo(); // member function declaration outside class. No good.

Simply remove the semicolon.

when i do that it gives me another error that states “expected a ‘;’” which leaves me with the same problem as i can not remove the ‘;’. else the function will not work at all

void UBullCowCartridge::InitGame()

{

// welcoming the player

PrintLine(TEXT("Welcome to Ezra's bullcow game!"));

PrintLine(TEXT("guess the %i letter word."), HiddenWord.Len());

PrintLine(TEXT("type in your guess and press enter to"));

PrintLine(TEXT("continue...")); // prompt player for guess

HiddenWord = TEXT("cakes");

Lives = 5;

bGameOver = false;

}

Please post the full error message.



Please post the actual compilation error from compiling along with your full code.

When I compile the code in Unreal, this is the error in the logs.

This is my full code:

#include “BullCowCartridge.h”

void UBullCowCartridge::BeginPlay() // When the game starts

{

Super::BeginPlay();



InitGame();

PrintLine(TEXT("the Hidden Word is: %s"), *HiddenWord); // debug messages

}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter

{

// if the game is over than do ClearScreen() and Initgame() the game

// else checking PlayerGuess

if (bGameOver)

{

    ClearScreen();

    InitGame();

}

else

{

    if (Input == HiddenWord)

{

     PrintLine(TEXT("You have won!"));

    bGameOver = true;

}

else

{

    if (Input.Len() != HiddenWord.Len())

    {

        PrintLine(TEXT("oooo, that's too many or too little words dumbass, try again"));

    }



    PrintLine(TEXT("You're trash kid, go home!"));

    bGameOver = true;

}

   

// Check if isogram

// prompt to guess again

// Check right number of characters

// prompt to guess again

 

// Remove Life

// Check if Lives > 0

// If yes GuessAgain

// show lives left

// if no show gameover and HiddenWord?

// prompt to play again, press enter to play again?

// check user input

// PlayAgain or Quit

}

void UBullCowCartridge::InitGame()

{

// welcoming the player

PrintLine(TEXT("Welcome to Ezra's bullcow game!"));

PrintLine(TEXT("guess the %i letter word."), HiddenWord.Len());

PrintLine(TEXT("type in your guess and press enter to"));

PrintLine(TEXT("continue...")); // prompt player for guess

HiddenWord = TEXT("cakes");

Lives = 5;

bGameOver = false;

}

That says you tried to create a function within another function. Fixing indentation you have the following.

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    // if the game is over than do ClearScreen() and Initgame() the game
    // else checking PlayerGuess
    if (bGameOver)
    {
        ClearScreen();
        InitGame();
    }
    else
    {
        if (Input == HiddenWord)
        {
            PrintLine(TEXT("You have won!"));
            bGameOver = true;
        }
        else
        {
            if (Input.Len() != HiddenWord.Len())
            {
                PrintLine(TEXT("oooo, that's too many or too little words dumbass, try again"));
            }
            PrintLine(TEXT("You're trash kid, go home!"));
            bGameOver = true;
        }
        // Check if isogram
        // prompt to guess again
        // Check right number of characters
        // prompt to guess again
        // Remove Life
        // Check if Lives > 0
        // If yes GuessAgain
        // show lives left
        // if no show gameover and HiddenWord?
        // prompt to play again, press enter to play again?
        // check user input
        // PlayAgain or Quit
    }

    void UBullCowCartridge::InitGame()
    {
        // welcoming the player
        PrintLine(TEXT("Welcome to Ezra's bullcow game!"));
        PrintLine(TEXT("guess the %i letter word."), HiddenWord.Len());
        PrintLine(TEXT("type in your guess and press enter to"));
        PrintLine(TEXT("continue...")); // prompt player for guess
        HiddenWord = TEXT("cakes");
        Lives = 5;
        bGameOver = false;
    }
// ???

Which is doing this

void Foo()
{
    void Bar() // defining a function within a function
    {
    }
}

You’re missing a } somewhere.

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

Privacy & Terms