Thread: Need help with templates

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    Question Need help with templates

    Hi. I'm trying to run a very simple program, using templates, which can do 2 things: sort an array, or reverse its elements. When I execute it, I get part of the answer, but also some weird numbers and/or letters. Any help much appreciated! (Oh, I'm using Microsoft Visual studio C++ 6)

    This is my .h file:
    Code:
    #ifndef TEMPLATE_H
    #define TEMPLATE_H
    
    
    template <class ANYTYPE>
    class TEMPLATE
    {
    	public:
    		void Sort(ANYTYPE x[]);
    		void Reverse(ANYTYPE x[]);
    };
    
    
    #endif

    This is my source.cpp file
    Code:
    template <class ANYTYPE>
    void TEMPLATE<ANYTYPE>::Sort(ANYTYPE x[])
    {
    	ANYTYPE i, j, imax;
    	ANYTYPE tempsortarray;
    
    	for(j = 4; j >= 2; j--)
    	{
    		imax = 0;
    		for(i = 0; i < j; i++)
    		{
    			if(x[i] > x[imax])
    			{imax = i;}
    		}
    
    		tempsortarray = x[imax];
    		x[imax] = x[j-1];
    		x[j-1] = tempsortarray;
    	    cout << x[i] << " ";
    	}
    }
    
    
    
    template <class ANYTYPE>
    void TEMPLATE<ANYTYPE>::Reverse(ANYTYPE x[])
    {
    	ANYTYPE tempsortarray;
    	for(ANYTYPE i = 0; i < 4/2; i++)
    	{
    		tempsortarray = x[i];
    		x[i] = x[4 - i - 1];
    		x[4 - i - 1] = tempsortarray;
    	    cout << x[i] << " ";
    	}
    }


    This is my main.cpp file
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Template.h"
    #include "Templatesource.cpp"
    
    int main()
    {
    	TEMPLATE <int> x_int;
    	TEMPLATE <char> y_char;
    	TEMPLATE <float> z_float;
    	int a[4]={8, 6, 4, 2};
    	int b[4]={1, 3, 5, 7};
    	char c[4]={'a', 'b', 'c', 'd'};
    	char d[4]={'l', 'm', 'n', 'o'};
    	float e[4]={1.5, 2.5, 3.5, 4.5};
        float f[4]={6.9, 7.9, 8.9, 9.9};
    
    	x_int.Sort(a);
        y_char.Reverse(c);
    	
    	return 0;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    didnt even look at the code. You have it split into three files. Your compiler will not be able to find the template definitions. Dont split the defs from the decs. i.e. put all the template code in the .h file then see if it compiles.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Actually, that's the way I'm supposed to do it, I'm afraid I don't have an option there. But anyway, it DOES compile and it DOES run, I just get some weird numbers along with the output, as I'm forgetting to initialize something. It shows correct results, and then some weird numbers.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A couple things. The first is that many compilers don't like template declarations/definitions in different files, so you might want to try put them into the same (header) file. Also, all of your index variables are of the type specified by the template, but in reality, they should all simply be ints. Probably not the cause of your problem, but it would definitely cause errors if you used this class with floats, or your own classes for example.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM