Template Class Problem - Newbie [modified]
-
Hi guys I'm trying to figure out how to make a template class History. This class should be able to record history moves.(for any application). e.g. a history of changed in an editor, or a history of URLs in a web browser etc. This is what i came up with so far.
#include < stack > #include < iostream > #include < string > using namespace std; template < typename E > class history { private: //i'm using to stacks to record history stack < E > back_list; stack < E > forward_list; public: history(); void add_entry(const E & entry); E & undo(); E & redo(); bool no_undo(); bool no_redo(); }; template < typename E > void history < E >::add_entry(const E & entry) { back_list.push(entry); while(!forward_list.empty()) { forward_list.pop(); } } template < typename E > E & history < E >::undo() { E entry; if(!back_list.empty()) { entry = back_list.top(); back_list.pop(); forward_list.push(entry); } return(entry); } template < typename E > E & history < E >::redo() { E entry; if(!forward_list.empty()) { entry = forward_list.top(); forward_list.pop(); back_list.push(entry); } return(entry); } template < typename E > bool history < E >::no_undo() { if(back_list.empty()) return(1); else return (0); } template < typename E > bool history < E >::no_redo() { if(forward_list.empty()) return (1); else return (0); } void main() { history <int> his; his.add_entry(2); int out; out = his.undo(); cout< I get no compilation errors but as soon as i try to run it i get: `Linking... history.obj : error LNK2001: unresolved external symbol "public: __thiscall history<int>::history<int>(void)" (??0?$history@H@@QAE@XZ) Debug/history.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. history.exe - 2 error(s), 0 warning(s)` Any ideas of i'm doing wrong? It's the first time i'm using template so i'll appreciate any help. Thank in advance. -- modified at 4:43 Friday 2nd March, 2007
-
Hi guys I'm trying to figure out how to make a template class History. This class should be able to record history moves.(for any application). e.g. a history of changed in an editor, or a history of URLs in a web browser etc. This is what i came up with so far.
#include < stack > #include < iostream > #include < string > using namespace std; template < typename E > class history { private: //i'm using to stacks to record history stack < E > back_list; stack < E > forward_list; public: history(); void add_entry(const E & entry); E & undo(); E & redo(); bool no_undo(); bool no_redo(); }; template < typename E > void history < E >::add_entry(const E & entry) { back_list.push(entry); while(!forward_list.empty()) { forward_list.pop(); } } template < typename E > E & history < E >::undo() { E entry; if(!back_list.empty()) { entry = back_list.top(); back_list.pop(); forward_list.push(entry); } return(entry); } template < typename E > E & history < E >::redo() { E entry; if(!forward_list.empty()) { entry = forward_list.top(); forward_list.pop(); back_list.push(entry); } return(entry); } template < typename E > bool history < E >::no_undo() { if(back_list.empty()) return(1); else return (0); } template < typename E > bool history < E >::no_redo() { if(forward_list.empty()) return (1); else return (0); } void main() { history <int> his; his.add_entry(2); int out; out = his.undo(); cout< I get no compilation errors but as soon as i try to run it i get: `Linking... history.obj : error LNK2001: unresolved external symbol "public: __thiscall history<int>::history<int>(void)" (??0?$history@H@@QAE@XZ) Debug/history.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. history.exe - 2 error(s), 0 warning(s)` Any ideas of i'm doing wrong? It's the first time i'm using template so i'll appreciate any help. Thank in advance. -- modified at 4:43 Friday 2nd March, 2007
you don't define a constructor for you class maybe, no ? also next time, please use the
<pre></pre>
tags, and replace the<
and>
with<
and>
[VisualCalc][Flags Beginner's Guide][CommDialogs new! ] | [Forums Guidelines]
-
Hi guys I'm trying to figure out how to make a template class History. This class should be able to record history moves.(for any application). e.g. a history of changed in an editor, or a history of URLs in a web browser etc. This is what i came up with so far.
#include < stack > #include < iostream > #include < string > using namespace std; template < typename E > class history { private: //i'm using to stacks to record history stack < E > back_list; stack < E > forward_list; public: history(); void add_entry(const E & entry); E & undo(); E & redo(); bool no_undo(); bool no_redo(); }; template < typename E > void history < E >::add_entry(const E & entry) { back_list.push(entry); while(!forward_list.empty()) { forward_list.pop(); } } template < typename E > E & history < E >::undo() { E entry; if(!back_list.empty()) { entry = back_list.top(); back_list.pop(); forward_list.push(entry); } return(entry); } template < typename E > E & history < E >::redo() { E entry; if(!forward_list.empty()) { entry = forward_list.top(); forward_list.pop(); back_list.push(entry); } return(entry); } template < typename E > bool history < E >::no_undo() { if(back_list.empty()) return(1); else return (0); } template < typename E > bool history < E >::no_redo() { if(forward_list.empty()) return (1); else return (0); } void main() { history <int> his; his.add_entry(2); int out; out = his.undo(); cout< I get no compilation errors but as soon as i try to run it i get: `Linking... history.obj : error LNK2001: unresolved external symbol "public: __thiscall history<int>::history<int>(void)" (??0?$history@H@@QAE@XZ) Debug/history.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. history.exe - 2 error(s), 0 warning(s)` Any ideas of i'm doing wrong? It's the first time i'm using template so i'll appreciate any help. Thank in advance. -- modified at 4:43 Friday 2nd March, 2007
antonaras wrote:
public: history();
Yes, you have forgot to define this c'tor.
Prasad Notifier using ATL | Operator new[],delete[][^]
-
you don't define a constructor for you class maybe, no ? also next time, please use the
<pre></pre>
tags, and replace the<
and>
with<
and>
[VisualCalc][Flags Beginner's Guide][CommDialogs new! ] | [Forums Guidelines]
-
Hi toxcct Thanks for helping me. Sorry about my post it was missing all the . U r right i don't define a constructor. Think thats why i get the errors? The default constructor history() won't do the job? Thanks again toxcct
antonaras wrote:
Think thats why i get the errors?
yes, that's exactly what the error message talks about :
unresolved external symbol "public: __thiscall
history<int>::history<int>
(void)"
[VisualCalc][Flags Beginner's Guide][CommDialogs new! ] | [Forums Guidelines]
-
antonaras wrote:
public: history();
Yes, you have forgot to define this c'tor.
Prasad Notifier using ATL | Operator new[],delete[][^]