ClassTemplates

Hi
I'm a beginner.
I'm struggling with class templates and can't seem to find my error.

//Header File (Calculator.h)

template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator();
Calculator(T,T);
void setA(int a){first = a;}
void setB(int b){second = b;}
T getA(){return first;}
T getB(){return second;}

T sum();
T difference();
T product();
T quotient();
};

//Implementation file Calculator.cpp

#include "Calculator.h"
#include <iostream>

using namespace std;
template<class T>
Calculator<T>::Calculator()
{

}

template <class T>
Calculator<T>::Calculator(T a, T b)
{
first = a;
second = b;
}

template <class T>
T Calculator<T>::sum()
{
return (first + second);
}

template <class T>
T Calculator<T>::difference()
{
return first - second;
}

template <class T>
T Calculator<T>::product()
{
return first * second;
}

template <class T>
T Calculator<T>::quotient()
{
return first/second;
}


//Run File TestCalculator.cpp
#include "Calculator.h"
#include <iostream>

using namespace std;
int main()
{
Calculator<int> v(100,25);

cout << "Sum: " << v.sum() << endl;
cout << "Difference: " << v.difference() << endl;
cout << "Product: " << v.product() << endl;
cout << "Quotient: " << v.quotient() << endl;

system("pause");
return 0;

}
This is the error i'm getting by sum,difference,product and quotient.
Can someone please help me?

Error 5 error LNK2019: unresolved external symbol "public: int __thiscall Calculator<int>::sum(void)" (?sum@?$Calculator@H@@QAEHXZ) referenced in function _main F:\TP preparation\ClassTemplates prep\ClassTemplates prep\TestCalculator.obj ClassTemplates prep

With template classes, you need to put the definitions of the methods in the header file. It's annoying, but you need to do it, because the full definition of the class needs to be available to the compiler at the point where it compiles the code that uses the template.
Last edited on
Meaning i need to put template<class T> above all my methods?
So do i need an implementation file?

In linux it works if you #import the cpp file in the header but visual studio doesn't seem to work
Last edited on
yaaz32 wrote:
Meaning i need to put template<class T> above all my methods?
No, define them inside the class definition.
yaaz32 wrote:
So do i need an implementation file?
No.
#calculator.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator();
Calculator(T,T);
void setA(int a){first = a;}
void setB(int b){second = b;}
T getA(){return first;}
T getB(){return second;}

T sum();
T difference();
T product();
T quotient();
};

#include "calculator.inl"


"calculator.inl"

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
using namespace std;
template<class T>
Calculator<T>::Calculator()
{

}

template <class T>
Calculator<T>::Calculator(T a, T b)
{
first = a;
second = b;
}

template <class T>
T Calculator<T>::sum()
{
return (first + second);
}

template <class T>
T Calculator<T>::difference()
{
return first - second;
}

template <class T>
T Calculator<T>::product()
{
return first * second;
}

template <class T>
T Calculator<T>::quotient()
{
return first/second;
}
I personally do not recommend the above method.

I recommend the below method instead, though obviously with proper indentation.
Last edited on
Solution 2:


calculator.hpp:

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

#include <iostream>

template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator()
{
    // define inside the class
}
Calculator(T,T)
{
    // define inside the class
}
void setA(int a)
{first = a;}
void setB(int b)
{second = b;}
T getA()
{return first;}
T getB()
{return second;}

T sum()
{
    // define inside the class
}
T difference()
{
    // define inside the class
}
T product()
{
    // define inside the class
}
T quotient()
{
    // define inside the class
}
};
Solution 3

calculator.hpp

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
#include <iostream>

template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator();
Calculator(T,T);
void setA(int a){first = a;}
void setB(int b){second = b;}
T getA(){return first;}
T getB(){return second;}

T sum();
T difference();
T product();
T quotient();
};

template <class T>
T Calculator<T>::sum()
{
return (first + second);
}

template <class T>
T Calculator<T>::difference()
{
return first - second;
}

template <class T>
T Calculator<T>::product()
{
return first * second;
}

template <class T>
T Calculator<T>::quotient()
{
return first/second;
}




ohhh and remeber put

#ifndef my_header_hpp
#define my_header_hpp


.... here your header

#endif


Thanks monochrome.
I have tried #include "calculator.cpp" but it didn't work.

I have to have an implementation file too LB. lecturers rules:(
@yaaz32

hehe, np

remember: you can not define inline or template funtion into *.cpp
Thank you for your help guys. Much appreciated. I will test it now:)
> I personally do not recommend the above method.
at least give rationale


> you can not define template funtion into *.cpp
yes, you can
explicit instantiation http://www.cplusplus.com/forum/articles/14272/

> I personally do not recommend the above method.
at least give rationale


> you can not define template funtion into *.cpp
yes, you can
explicit instantiation http://www.cplusplus.com/forum/articles/14272/


wow thanks o.o
Topic archived. No new replies allowed.