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. What is wrong with this template?

What is wrong with this template?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestionc++database
3 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.
  • S Offline
    S Offline
    sacoskun
    wrote on last edited by
    #1

    StackA.h #ifndef STACKA_H #define STACKA_H // ********************************************************* // Header file StackA.h for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackException.h" #include const int MAX_STACK = 100; template < class StackItemType > class Stack { public: // constructors and destructor: Stack(); // default constructor // stack operations: bool isEmpty() const; void push(StackItemType newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException); private: StackItemType items[MAX_STACK]; // array of stack items int top; // index to top of stack }; // end class #endif StackA.cpp // ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file #include "StackException.h" template < class StackItemType > Stack::Stack(): top(-1) { } // end default constructor template < class StackItemType > bool Stack::isEmpty() const { return top < 0; } // end isEmpty template < class StackItemType > void Stack::push(StackItemType newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push template < class StackItemType > void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else --top; // stack is not empty; pop top } // end pop template < class StackItemType > void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop template < class StackItemType > void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty())

    C J 2 Replies Last reply
    0
    • S sacoskun

      StackA.h #ifndef STACKA_H #define STACKA_H // ********************************************************* // Header file StackA.h for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackException.h" #include const int MAX_STACK = 100; template < class StackItemType > class Stack { public: // constructors and destructor: Stack(); // default constructor // stack operations: bool isEmpty() const; void push(StackItemType newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException); private: StackItemType items[MAX_STACK]; // array of stack items int top; // index to top of stack }; // end class #endif StackA.cpp // ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file #include "StackException.h" template < class StackItemType > Stack::Stack(): top(-1) { } // end default constructor template < class StackItemType > bool Stack::isEmpty() const { return top < 0; } // end isEmpty template < class StackItemType > void Stack::push(StackItemType newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push template < class StackItemType > void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else --top; // stack is not empty; pop top } // end pop template < class StackItemType > void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop template < class StackItemType > void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty())

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      this might help[^] Software | Cleek

      1 Reply Last reply
      0
      • S sacoskun

        StackA.h #ifndef STACKA_H #define STACKA_H // ********************************************************* // Header file StackA.h for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackException.h" #include const int MAX_STACK = 100; template < class StackItemType > class Stack { public: // constructors and destructor: Stack(); // default constructor // stack operations: bool isEmpty() const; void push(StackItemType newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException); private: StackItemType items[MAX_STACK]; // array of stack items int top; // index to top of stack }; // end class #endif StackA.cpp // ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file #include "StackException.h" template < class StackItemType > Stack::Stack(): top(-1) { } // end default constructor template < class StackItemType > bool Stack::isEmpty() const { return top < 0; } // end isEmpty template < class StackItemType > void Stack::push(StackItemType newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push template < class StackItemType > void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else --top; // stack is not empty; pop top } // end pop template < class StackItemType > void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop template < class StackItemType > void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty())

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        All those template functions (in StackA.cpp) used by the templated class must be in the header file not in a seperate CPP file. Otherwise the compiler only has the prototypes for functions it has not generated. Therefore, you will get "unresolved external...", because they do not exist. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

        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