Friday, 15 March 2013

c++ - Link Error in class template -



c++ - Link Error in class template -

please have @ next code

calculator.h

#pragma 1 time #include <iostream> template<class t> class calculator { public: calculator(void); ~calculator(void); void add(t x, t y) { cout << (x+y) << endl; } void min(t x, t y) { cout << (x-y) << endl; } void max(t x, t y) { cout << (x*y) << endl; } void dev(t x, t y) { cout << (x/y) << endl; } };

main.cpp

#include "calculator.h" using namespace std; int main() { calculator<double> c; c.add(23.34,21.56); system("pause"); homecoming 0; }

when run code, below error. not much familiar class templates. please help!

1>------ build started: project: templatecalculator, configuration: debug win32 ------ 1>link : error lnk2001: unresolved external symbol _maincrtstartup 1>c:\users\yohan\documents\visual studio 2010\projects\templatecalculator\debug\templatecalculator.exe : fatal error lnk1120: 1 unresolved externals ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

threw vs2010 quick. not liking cout , endl due not using namespace std. set in using statement speed. prefer utilize std:: conventions because avoids naming issues 'using' can generate

also complained no constructor , destructor (added in {}'s)

and previous comment said, having main in header file bad too. in default project in test.cpp

edit changed file construction have calculator in .h

calculator.h

template<class t> class calculator { public: calculator(void) { } ~calculator(void) { } void add(t x, t y) { cout << (x+y) << endl; } void min(t x, t y) { cout << (x-y) << endl; } void max(t x, t y) { cout << (x*y) << endl; } void dev(t x, t y) { cout << (x/y) << endl; } private: t numbers[2]; };

test.cpp

// test.cpp : defines entry point console application. // #include "stdafx.h" #pragma 1 time #include <iostream> using namespace std; #include "calculator.h" int main() { calculator<double> c; c.add(23.34,21.56); system("pause"); homecoming 0; }

c++ visual-studio-2010 templates

No comments:

Post a Comment