void foo::setFoo(T val){ f = val; } T foo::getFoo(){ return f;"> void foo::setFoo(T val){ f = val; } T foo::getFoo(){ return f;">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

foo.h #ifndef _FOO #define _FOO template <class T> class foo{

Similar presentations


Presentation on theme: "foo.h #ifndef _FOO #define _FOO template <class T> class foo{"— Presentation transcript:

1 foo.h #ifndef _FOO #define _FOO template <class T> class foo{
private: T f; public: void setFoo(T val); T getFoo(); }; #endif

2 foo.cpp #include "foo.h" template <class T>
void foo<T>::setFoo(T val){ f = val; } T foo<T>::getFoo(){ return f;

3 fooImpl.cpp #include "foo.cpp" template foo<int>;

4 goodtestFoo.cpp #include <iostream> using namespace std;
#include "fooImpl.cpp" int main(){ foo<int> myFoo; myFoo.setFoo(42); cout << "we got " << myFoo.getFoo() << endl; return 0; }

5 badTestFoo.cpp #include <iostream> using namespace std;
#include "fooImpl.cpp" int main(){ foo<int> myFoo; myFoo.setFoo(42); cout << "we got " << myFoo.getFoo() << endl; foo<double> anotherFoo; //OOPS! – not instantiated anotherFoo.set(64.5); cout << "we got " << anotherFoo.getFoo() << endl; return 0; }

6 compile, run goodtestfoo
linux3[106] % g++ -o goodtestfoo goodtestfoo.cpp fooImpl.cpp linux3[107] % goodtestfoo we got 42

7 compiling badtestfoo linux3[104] % g++ -o badtestfoo badtestfoo.cpp fooImpl.cpp badtestfoo.cpp: In function `int main()': badtestfoo.cpp:10: no matching function for call to `foo<double>::set(double)'


Download ppt "foo.h #ifndef _FOO #define _FOO template <class T> class foo{"

Similar presentations


Ads by Google