Template Classes program example won't compile

Hello everybody; I was doing a little program that I found in a programming book, just to practice a little, but I don't understand why it won't compile in my MS Visual Studio 2012. The error says: "error LNK1120: 6 unresolved externals".
I checked my code many times and now I'm out of ideas =/

Here is my code:
[NOTE: I'm aware that my method design is a little odd, but I did it this way because the template functions Problem(), Solution() and Congrats() used to be global template functions and I decided to change them into a template class.
Also, if you ask me "why do you print the answer 10 times?" my answer is: "why not?"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// TemplatedMathProblem.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;

template <class T>
class CTemplatedMathProblem
{
    private:
        // =====================================================================
        // Attributes
        // =====================================================================
        static char c_equals; // The '=' character
        char c_operand;       // The operator for the current problem
        T First;              // The 1st operand of the operation
        T Second;             // The 2nd operand of the operation
        T Answer;             // The user's answer
        T Result;             // The correct answer to the problem

        // =====================================================================
        // Private Methods
        // =====================================================================
        /* Display the arguments in the same order on the screen. Then it
        prompts the user to input the correct answer and, after that, it returns
        the input.
        */
        T Problem(T, char, T);

        /* It performs the desired operation between the first and second
        arguments in order to calculate the correct result of the problem. It
        returns the calculated correct answer.
        */
        T Solution(T, char, T);

        /* This function receives the correct answer and will display it
        alongside a congratulatory message 10 times.
        */
        void Congrats(T ans) {
            for (int i = 0; i < 10; i++)
                cout << ans << " CONGRATULATIONS! Correct answer!" << endl;
        }

    public:
        // =====================================================================
        // Public Methods
        // =====================================================================
        /* Display the mathematical problem on the screen and inputs the user's
         * answer
         */
        void DisplayProblem();

        /* Checks if the user's answer is correct and display a message 
        correspondingly
        */
        void CheckAnswer() const;

        // =====================================================================
        // Constructor & Destructor
        // =====================================================================
        /* Initialize the object's attributes with the values passed by argument
        */
        CTemplatedMathProblem(T, T, char op = '+');

        // Destructor
        virtual ~CTemplatedMathProblem() {}
};

// TemplatedMathProblem.cpp
#include "TemplatedMathProblem.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

// The '=' character
template <class T>
char CTemplatedMathProblem<T>::c_equals = '=';

// =============================================================================
// Constructor & Destructor
// =============================================================================
// Initialize the object's attributes with the values passed by argument
template <class T>
CTemplatedMathProblem<T>::CTemplatedMathProblem(T a, T b, char op):
c_operand(op), First(a), Second(b), Answer(), Result()
{}

// =============================================================================
// Private Methods
// =============================================================================
/* Display the arguments in the same order on the screen. Then it
prompts the user to input the correct answer and, after that, it returns
the input.
*/
template <class T>
T CTemplatedMathProblem<T>::Problem(T first, char operand, T second)
{
    bool b_inputIsValid; // flag that checks if the input is valid
    T answer;            // the user's answer

    // First, we display the problem
    cout << "Please solve the next problem: " << endl;
    cout << first << " " << operand << " " << second << c_equals << endl;

    // Then, we receive the input, checking that the input is a valid value
    do {
        cin >> answer;
        if (cin.fail()) {
            cout << "Sorry! Invalid input, please try again" << endl << endl;
            cin.clear();
            cin.sync();
            b_inputIsValid = false;
        }
        else
            b_inputIsValid = true;
    } while (!b_inputIsValid);

    // And finally, we return the answer
    return answer;
}

/* It performs the desired operation between the first and second
arguments in order to calculate the correct result of the problem. It
returns the calculated correct answer.
*/
template <class T>
T CTemplatedMathProblem<T>::Solution(T first, char operand, T second)
{
    T result; // The storage for the solution
    
    // We need to know which operation is in order to calculate the result
    switch (operand) {
        case '+': result = first + second; break;
        case '-': result = first - second; break;
        case '*': result = first * second; break;
        case '/': result = first / second; break;
        default: result = first + second; break;
    }

    // And we return the result
    return result;
}

// =============================================================================
// Public Methods
// =============================================================================
// Display the mathematical problem on the screen and inputs the user's answer  
template <class T>
void CTemplatedMathProblem<T>::DisplayProblem()
{
    Answer = Problem(First, c_operand, Second);
    Result = Solution(First, c_operand, Second);
}

// Checks if the user's answer is correct and display a message correspondingly
template <class T>
void CTemplatedMathProblem<T>::CheckAnswer() const
{
    if (Answer == Result) 
        Congrats();
    else
        cout << "Sorry, incorrect answer :(" << endl;
}

// main.cpp
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include "TemplatedMathProblem.h"

int main()
{
    // The math problem
    CTemplatedMathProblem<int> Problem(10, 5);

    // We display the problem and input our answer
    Problem.DisplayProblem();
    Problem.CheckAnswer();

    // And we end the program
    cout << endl << "Press an key...";
    cin.sync();
    cin.get();
    return 0;
}


I hope you guys can help me =D
Last edited on
For those who want to know the debugger's output and/or can UNDERSTAND it, I post it here for you:
1
2
3
4
5
6
7
8
9
10
1
1>  TemplatedMathProblem.cpp
1>  main.cpp
1>  Generating Code...
1>main.obj : error LNK2028: unresolved token (0A0003DC) "public: void __thiscall CTemplatedMathProblem<int>::DisplayProblem(void)" (?DisplayProblem@?$CTemplatedMathProblem@H@@$$FQAEXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2028: unresolved token (0A0003DD) "public: void __thiscall CTemplatedMathProblem<int>::CheckAnswer(void)const " (?CheckAnswer@?$CTemplatedMathProblem@H@@$$FQBEXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2028: unresolved token (0A0003DE) "public: __thiscall CTemplatedMathProblem<int>::CTemplatedMathProblem<int>(int,int,char)" (??0?$CTemplatedMathProblem@H@@$$FQAE@HHD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CTemplatedMathProblem<int>::CTemplatedMathProblem<int>(int,int,char)" (??0?$CTemplatedMathProblem@H@@$$FQAE@HHD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CTemplatedMathProblem<int>::DisplayProblem(void)" (?DisplayProblem@?$CTemplatedMathProblem@H@@$$FQAEXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CTemplatedMathProblem<int>::CheckAnswer(void)const " (?CheckAnswer@?$CTemplatedMathProblem@H@@$$FQBEXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Users\XXX\Documents\Visual Studio 2012\Projects\Lessons\Lesson_12\Debug\Project1.exe : fatal error LNK1120: 6 unresolved externals
Last edited on
Templates go into the headers - move the contents of your TemplatedMathProblem.cpp into TemplatedMathProblem.h
Oh, hahaha! Thanks man, very appreciated =)
Topic archived. No new replies allowed.