Thread: Class template, my first attempt

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    Class template, my first attempt

    Im getting linker errors when trying to create a class template. I can't seem to see what is bad with this code. Any help would be great, thanks.

    in main.cpp:
    Code:
    // main.cpp : practice.
    
    #include <iostream>
    using namespace std;
    
    #include "Stack.h"
    
    /***********/
    /* Globals */
    /***********/
    
    /**************/
    /* Prototypes */
    /**************/
    
    /************/
    /* Internal */
    /************/
    
    // Our primary routine.  This is called when the program starts.
    //
    // Return:	We always return 0.
    int main(void)
    {
    
    	CStack<int> values;
    
    	int num = 4;
    
    
    	if (values.isEmpty())
    		cout << "Values is empty..\n";
    	else
    		cout << "Values has an item in it..\n";
    
    	num = 5;
    	cout << "Adding 5 to CStack values...\n";
    	values.push_back(num);
    
    	num = 3;
    	cout << "Adding 3 to CStack values...\n";
    	values.push_back(num);
    
    	cout << "Taking one out.\n";
    	values.pop(num);
    	cout << num << "Removed..\n";
    	
    }
    in Stack.h
    Code:
    #pragma once
    
    template <typename Type>
    class CStack
    {
    private:
    	static const int MAX = 10;
    	Type items[MAX];
    	int top;
    public:
    	CStack(void);
    	bool isFull(void) const;
    	bool isEmpty(void) const;
    	bool push_back(const Type & item);
    	bool pop(Type & item);
    };
    in Stack.cpp
    Code:
    #include "Stack.h"
    
    template <typename Type>
    CStack<Type>::CStack(void)
    {
    	top = 0;
    }
    
    template <typename Type>
    bool CStack<Type>::isFull(void) const
    {
    	return top == MAX;
    }
    
    template <typename Type>
    bool CStack<Type>::isEmpty(void) const
    {
    	return top == 0;
    }
    
    template <typename Type>
    bool CStack<Type>::push_back(const Type & item)
    {
    	if (top < max)
    	{	
    		items[top++] = item;
    		return true;
    	}
    	else
    		return false;
    }
    
    template <typename Type>
    bool CStack<Type>::pop(Type & item)
    {
    	if (top > 0)
    	{
    		item = items[--top];
    		return true;
    	}
    	else
    		return false;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There are plenty of threads about this problem on this forum.
    Short Answer:
    Put the code from Stack.cpp into Stack.h and delete Stack.cpp.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM