How to make proper use of Interface/Abstract classes?

I come from a programming background in Unity3D and I’m trying to teach myself C++ through Unreal. For my current project, I made a quick prototype in Unity just to have the logic figured out and now I’m basically trying to translate C# into C++. I’m stuck on proper coding conventions regarding C++ as well as adhering to the conventions laid out in Unreal.

In my Unity prototype, I made a series of puzzles which you must press certain keys to find a correct solution for. I have 8 unique puzzles in total that I made each into separate classes to help better organize the randomization. Because of the nature of C#, I made an interface IPuzzle with a concrete method IsSolutionCorrect(). This interface is inherited onto my 8 other Puzzle classes because in each one, IsSolutionCorrect() is going to have a completely different implementation for checking user input.

The user is presented with 2 puzzles each from the pool of 8 randomly which counts as a set. I made 2 variables out of the interface, IPuzzle puzzleA; IPuzzle puzzleB;, and I randomly assign one of my 8 puzzle classes that inherit the interface from a list of my 8 instantiated puzzle classes, List puzzleList = new List(); puzzleList.Add(new Puzzle1A()) ...; puzzleA = GetRandomPuzzleFromList();. Once puzzleA and puzzleB have a reference to a particular puzzle, then when I go to check the solution I call puzzleA.IsSolutionCorrect() and that way I don’t need to worry about class types so as long as that interface is there.

I’m struggling to replicate this same behavior in C++.

This is my header

namespace Puzzles{

class IPuzzle{
public:
	std::string id;
	virtual bool IsSolutionCorrect();
};

class Puzzle1A : public IPuzzle{
public:
	Puzzle1A();
	bool IsSolutionCorrect() override;
};

   class Puzzle1B : public IPuzzle{
public:
	Puzzle1B();
	bool IsSolutionCorrect() override;
};

   ...

}

This is my source

namespace Puzzles{

Puzzle1A::Puzzle1A(){
	id = "1A";
}

bool Puzzle1A::IsSolutionCorrect(){
	return false;
}

Puzzle1B::Puzzle1B(){
	id = "1B";
}

bool Puzzle1B::IsSolutionCorrect(){
    //Todo: write implementation.
	return false;
}

    ...

}

I’m getting LNK2001 unresolved external symbol compile errors and I’m not sure what’s going wrong. It compiles correctly after I fiddle with the Binaries folder and then other times it randomly breaks with LNK2001. I have code commented out thats been problematic, yet for some reason it still continues to be problematic while commented. Yes, I’m remembering to save the source. Its like its trying to compile things that shouldn’t still exist.

If I’m trying to make a variable out of a common, base object that is expected to be inherited, do I still need to provide a constructor, deconstructor, and proper implementation even though all of that is expected to be overridden?

It just now compiled correctly without me changing anything. I literally just kept hitting the compile button. Yay?

I was going to say your syntax seems fine, but it looks like you manually made these classes, do you want them to be actual unreal classes that you can use in game?

I see you have it working, however I thought I’d provide an alternative way because UE4 actually has it’s own implementation for interfaces that you could benefit from, doing it that way is like reinventing the wheel as a hexagon. :slight_smile:

There’s a good wiki page by Rama on it here: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

It should give you a lot more flexibility and power as your system develops, whereas you might hit a wall or do a lot of extra work by making your own, after all, that’s why we have game engines :slight_smile: