Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a “looking for group” queue using a linked list, rather than a dynamic

ID: 3805594 • Letter: I

Question

Implement a “looking for group” queue using a linked list, rather than a dynamic array.
The following files have been given to you:
1. A C++ header file (lfgqueue.h) declaring the LFGQueue class.
2. A C++ header file (player.h) declaring and implementing the Player class.
3. A C++ source file (main.cpp) containing a main function with assertions.
Create a new C++ source file named lfgqueue.cpp that implements the LFGQueue class declared in lfgqueue.h such that main.cpp and lfgqueue.cpp compile into a program that runs with no failed assertions.
----------------------------------------------------------------------------------------------------
//lfgqueue.h

/*


edit here



*/

Explanation / Answer

If you try to implement the template class like a normal class implementation as shown above, it will generate a set of compilation errors such as:

Reason

In this case, the compiler doesn't know about the object type. So it will not compile.

Solution

To compile this class without any errors, you need to put the template specific declaration in a .cpp file, as shown below:

Template Class Header File

Template Class Source File

Linking Issue

With the above code, after resolving all the compilation errors, you may get some link errors while you create an object of this class in any file other than TestTemp.cpp. Here is some sample code:

Client Source File

Link Error

Hide   Copy Code

Reason

When the compiler encounters a declaration of a TestTemp object of some specific type, e.g., int, it must have access to the template implementation source. Otherwise, it will have no idea how to construct the TestTempmember functions. And, if you have put the implementation in a source (TestTemp.cpp) file and made it a separate part of the project, the compiler will not be able to find it when it is trying to compile the client source file. And, #includeing the header file (TestTemp.h) will not be sufficient at that time. That only tells the compiler how to allocate for the object data and how to build the calls to the member functions, not how to build the member functions. And again, the compiler won't complain. It will assume that these functions are provided elsewhere, and leave it to the linker to find them. So, when it's time to link, you will get "unresolved references" to any of the class member functions that are not defined "inline" in the class definition.

Solution

There are different methods to solve this problem. You can select from any of the methods below depending on which is suitable for your application design.

Mehtod 1

You can create an object of a template class in the same source file where it is implemented (TestTemp.cpp). So, there is no need to link the object creation code with its actual implementation in some other file. This will cause the compiler to compile these particular types so the associated class member functions will be available at link time. Here is the sample code:

Template Class Header File

Template Class Source Fild

Client Source File

The temporary function in "TestTemp.cpp" will solve the link error. No need to call this function because it's global.

Method 2

You can #include the source file that implements your template class in your client source file. Here is the sample code:

Template Class Header File

Template Class Source File

Client Source File

Method 3

You can #include the source file that implements your template class (TestTemp.cpp) in your header file that defines the template class (TestTemp.h), and remove the source file from the project, not from the folder. Here is the sample code:

Template Class Header File

Template Class Source File

Client Source File

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote