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?