Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template Class Problem - Newbie [modified]

Template Class Problem - Newbie [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresdebuggingtutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antonaras
    wrote on last edited by
    #1

    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

    T P 2 Replies Last reply
    0
    • A antonaras

      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

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      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 &lt; and &gt;


      [VisualCalc][Flags Beginner's Guide][CommDialogs new! ] | [Forums Guidelines]

      A 1 Reply Last reply
      0
      • A antonaras

        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

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        antonaras wrote:

        public: history();

        Yes, you have forgot to define this c'tor.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        A 1 Reply Last reply
        0
        • T toxcct

          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 &lt; and &gt;


          [VisualCalc][Flags Beginner's Guide][CommDialogs new! ] | [Forums Guidelines]

          A Offline
          A Offline
          antonaras
          wrote on last edited by
          #4

          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

          T 1 Reply Last reply
          0
          • A antonaras

            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

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            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]

            1 Reply Last reply
            0
            • P prasad_som

              antonaras wrote:

              public: history();

              Yes, you have forgot to define this c'tor.

              Prasad Notifier using ATL | Operator new[],delete[][^]

              A Offline
              A Offline
              antonaras
              wrote on last edited by
              #6

              I'm so stubit guys. You were right i forgot to define the constructor outside the class. It works fine now cheers everyone. Appreciate ur help

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups