dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Newbie need help with C++ template

  1. #1
    Join Date
    Jul 2005
    Posts
    24

    Newbie need help with C++ template

    Hello,

    I need help i am trying to compile some code using G++ compiler and i am receiving the following errors (see below). What am i doing wrong here, how can i get this to compile?


    Header:
    Code:
    /* 
     * File:   stack.h
     * Author: 
     *
     * Created on April 5, 2008, 7:36 PM
     */
    #include <dos.h>        // For sleep()
    #include <iostream>   // For I/O
    #include <windows.h>    // For MessageBox API
    #include <conio.h>
    
    #ifndef _STACK_H
    #define	_STACK_H
    #define MAX 10 // Maximum stack Content
    
    template <class T>  // Using Templates so that any type of data can be
                        // stored in Stack without multiple defination of class
    
    class Stack {
    public:    
        Stack();
        void Push(T a);
        T Pop();
        virtual ~Stack();
        T item,r;
        int top;        //Contains location of Topmost Data pushed onto Stack
    private:
    
    protected:
        T arr[MAX];     // Contains all the Data
    
    };
    
    #endif	/* _STACK_H */

    C++ Class
    Code:
    /* 
     * File:   stack.cpp
     * Author: 
     * 
     * Created on April 5, 2008, 7:36 PM
     */
    
    #include "stack.h"
    
    Stack::Stack() {
    
        for (int i = 0; i < MAX; i++) {
            arr[i] = NULL; //Initialises all Stack Contents to NULL
        }
    
        top = -1; //Sets the Top Location to -1 indicating an empty stack
    }
    
    Stack::~Stack(){
        
    }
    
    Stack::Push(T a) {
    
        top++; // increment to by 1
        if (top < MAX) {
            arr[top] = a; //If Stack is Vacant store Value in Array
        } else // Bug the User
        {
            MessageBox(0, "STACK IS FULL", "STACK WARNING!", MB_ICONSTOP);
            top--;
        }
    }
    
    Stack::Pop() {
    
        if (top == -1) {
            MessageBox(0, "STACK IS EMPTY","WARNING",MB_ICONSTOP);
            return NULL;
        } else {
            T data = arr[top];  //Set Topmost Value in data
            arr[top] = NULL; //Set Original Location to NULL
            top--;      // Decrement top by 1
            return data;        // Return deleted item
        }
    }


    Errors:
    Running "C:\MSYS\1.0\bin\make.exe -f Makefile CONF=Debug" in C:\Users\Willie Johnson Jr\Documents\NetBeansProjects\Stack_Templates

    /usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
    make[1]: Entering directory `/c/Users/Willie Johnson Jr/Documents/NetBeansProjects/Stack_Templates'
    /usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/stack_templates.exe
    make[2]: Entering directory `/c/Users/Willie Johnson Jr/Documents/NetBeansProjects/Stack_Templates'
    mkdir -p build/Debug/MinGW-Windows
    rm -f build/Debug/MinGW-Windows/stack.o.d
    g++.exe -c -g -MMD -MP -MF build/Debug/MinGW-Windows/stack.o.d -o build/Debug/MinGW-Windows/stack.o stack.cpp
    stack.h: In instantiation of `Stack<void>':

    stack.cpp:10: instantiated from here

    stack.h:22: error: invalid parameter type `void'

    stack.h:22: error: in declaration `void Stack<T>::Push(T)'

    stack.h:25: error: instantiation of `Stack<T>::item' as type `void'

    stack.h:25: error: `Stack<T>::item' has incomplete type

    stack.h:25: error: invalid use of `void'

    stack.h:25: error: instantiation of `Stack<T>::r' as type `void'

    stack.h:25: error: `Stack<T>::r' has incomplete type

    stack.h:25: error: invalid use of `void'

    stack.h:30: error: creating array of `void'

    stack.cpp:10: error: return type specification for constructor invalid

    stack.cpp: In constructor `Stack<T>::Stack() [with T = void]':

    stack.cpp:13: error: `arr' was not declared in this scope

    stack.cpp: At global scope:

    stack.cpp:19: error: expected constructor, destructor, or type conversion before '::' token

    stack.cpp:23: error: `template<class T> class Stack' used without template parameters

    stack.cpp:23: error: expected constructor, destructor, or type conversion before '(' token

    stack.cpp:35: error: `template<class T> class Stack' used without template parameters

    stack.cpp:35: error: ISO C++ forbids declaration of `Pop' with no type

    stack.cpp: In function `int Pop()':

    stack.cpp:37: error: `top' was not declared in this scope

    stack.cpp:39: warning: converting to non-pointer type `int' from NULL

    stack.cpp:41: error: `T' was not declared in this scope

    stack.cpp:41: error: expected `;' before "data"

    stack.cpp:42: error: `arr' was not declared in this scope

    stack.cpp:44: error: `data' was not declared in this scope

    make[2]: *** [build/Debug/MinGW-Windows/stack.o] Error 1
    make[2]: Leaving directory `/c/Users/Willie Johnson Jr/Documents/NetBeansProjects/Stack_Templates'
    make[1]: *** [.build-conf] Error 2
    make[1]: Leaving directory `/c/Users/Willie Johnson Jr/Documents/NetBeansProjects/Stack_Templates'
    make: *** [.build-impl] Error 2

    Build failed. Exit value 2.

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    #include "stack.h"

    this is the way to define members of template classes:
    Code:
    template< typename T >
    Stack<T>::Stack() {
    
        for (int i = 0; i < MAX; i++) {
            arr[i] = NULL; //Initialises all Stack Contents to NULL
        }
    
        top = -1; //Sets the Top Location to -1 indicating an empty stack
    }
    
    template< typename T >
    Stack<T>::~Stack(){
        
    }
    
    template< typename T >
    Stack<T>::Push(T a) {
    
    // etc
    and put the definition of template functions in the header file, not in a separate cpp file. the compiler needs to see these definitions when instantiating them.
    for more information, see: http://www.parashift.com/c++-faq-lit...html#faq-35.12

Similar Threads

  1. AA Tree Implementation Variation and Concepts
    By Peter_APIIT in forum C++
    Replies: 7
    Last Post: 12-09-2008, 01:32 AM
  2. Problem with Matrix template class
    By greenmile in forum C++
    Replies: 2
    Last Post: 10-16-2008, 10:09 PM
  3. template management of the website
    By anitha2324 in forum AJAX
    Replies: 0
    Last Post: 06-18-2008, 04:22 AM
  4. Replies: 0
    Last Post: 12-28-2005, 05:01 PM
  5. What's Template Metaprogramming?
    By topcoder in forum C++
    Replies: 2
    Last Post: 07-26-2005, 07:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center