Class Template

This is a discussion on Class Template within the C++ Programming forums, part of the General Programming Boards category; I am currently taking a data structures class and I've run into a snag regarding one of the projects. It's ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Class Template

    I am currently taking a data structures class and I've run into a snag regarding one of the projects. It's a big one, as every other project after it requires it.

    Class templates.

    I get the general idea, but whenever I try to implement it, something or another goes horribly wrong. I've been at it for a while now, and I've all but given up on it. Can anyone show me how to (in my professor's own words, typos and all):

    Include the template class implementation <class T> and replace int in the above class whenever required for T (warning due not change int for T whenever is not required).
    This is as far as I got.

    Code:
    ------------------array.h------------------
    
    template <typename T>
    class Array
    {
    public:
    	Array<T> (int numelems)
    	{
    		Alist = new T[numelems];
    		nume = numelems;
    		for (int i=0; i<nume; i++)
    			Alist[i]=0;
    		top = -1;
    	}
    
    	bool operator > (T a)
    	{
    		if (++top < nume)
    			Alist[top]=a;
    		if ((top+1) < nume)
    			return true;
    		else return false;
    	}
    
    	void displayArray();
    	void Sort();
    	~Array();
    
    private:
    	T* Alist;
    	int top;
    	int nume;
    };
    
    
    
    
    ------------------array.cpp------------------
    
    #include <iostream>
    #include "array.h"
    
    template <typename T>
    void Array<T>::displayArray()
    {
    	for (int i=0; i<nume; i++)
    		std::cout << Alist[i] << std::endl;
    }
    
    template <typename T>
    void Array<T>::Sort()
    {
    	T temp;
    	int check;
    	check=1;
    	while (check!=0)
    	{
    		check=0;
    		for (int i=0; i<(nume-1); i++)
    		{
    			if (Alist[i] > Alist[i+1])
    			{
    				check=1;
    				temp=Alist[i];
    				Alist[i]=Alist[i+1];
    				Alist[i+1]=temp;
    			}
    		}
    	}
    	return;
    }
    
    template <typename T>
    Array<T>::~Array()
    {
    	delete [] Alist;
    }
    
    
    
    
    ------------------usingarray.cpp------------------
    
    #include <iostream>
    #include "array.h"
    
    using namespace std;
    
    int main()
    {
    	int x, i;
    
    	cout << "Input size of the int array: ";
    	cin >> i;
    	Array<int> A_int(i);
    
    	cout << "Input elements into the int array: " << endl;
    	do {
    		cin >> x;
    	} while (A_int > x);
    	cout << "The array is full." << endl << endl;
    
    	A_int.Sort();
    
    	cout << "The sorted int array" << endl;
    	A_int.displayArray();
    	cout << endl;
    
    	return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    With templates you cannot separate declarations and implementations. It all needs to be available to any compilation unit that might need your template.

    Move everything in array.cpp to array.h and it should be fine.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    If only I could. My professor requires us to have two source files and one header. One source with the main program, one source with the implementation file, and then the header.

    If I move everything into the header, what goes into implementation?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Nothing. You cannot separate template declaration and implementation.
    You could include the source file from the header. However, I would not rely on it myself, but just so you're aware.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The best thing you can do is to include the template implementation from the end of the template header. You might also change the file extension (e.g to *.tpp) so as to make clear this file is not to be compiled separately.

    Your teacher has no say in this, as template code has to be all in the header one way or another.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Alright, I included it at the bottom of the header, and that seems to have done the trick...after I removed array.cpp from my source files. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 06:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21