Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Leiarchy9

What is wrong with my template function?

This topic is 5362 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Main.h
#ifndef _Main_h
#define _Main_h



template<class dataType>
class CArray
{

     dataType * _array;
     int _size;
     
     public:
     
        CArray(void): _size(20)
        {
        
           _array = new dataType[_size];
        }
        
        CArray(int paramSize): _size(paramSize)
        {
        
           _array = new dataType[_size];
        }
        
        ~CArray(void)
        {
        
           if( _array != NULL )
           {

              delete [] _array;
           }
           
           _array = NULL;
        }
        
        operator dataType*(void)
        {
        
           return _array;
        }
        
        dataType & operator[](int element)
        {
        
           return _array[element];
        }
        
        void resize(int newSize)
        {

           if( _array != NULL )
           {

              delete [] _array;
           }
           
           _array = NULL;
           _size = newSize;

           _array = new dataType[newSize];
        }
        
        void insert(int element, dataType value)
        {
        
           for( int index = _size - 1; index > element; index-- )
           {
           
              _array[index] = _array[index - 1];
           }
           
           _array[element] = value;
        }

        void clear(dataType zero)
        {
        
           for( int index = 0; index < _size; index++ )
           {
           
              _array[index] = zero;
           }
           
        }

        int GetSize(void) const
        {
        
           return _size;
        }
        
};


#endif
Main.cpp
#include <iostream>
#include <cstdlib>
#include <time.h>

#include "Main.h"

   using std::cout;
   using std::cin;
   using std::endl;
   
   template<typename dataType, const int MAX, dataType value>
   void Process(dataType*&);
   
   template<typename dataType, const int MAX>
   void Process2(dataType*);
   

   
int main(void)
{

     srand( (int)time(NULL) );
     CArray<int> array(9);

     Process<int, array.GetSize(), int(rand() % 10)>(array);
     
     cin.get();
     return EXIT_SUCCESS;
}


template<typename dataType, int MAX, dataType value>
void Process( dataType *& pArray )
{

     for( int index = 0; index < MAX; index++ )
     {
     
        pArray[index] = value;
     }

}


template<typename dataType, const int MAX>
void Process2( dataType * pArray )
{

     for( int index = 0; index < MAX; index++ )
     {
     
        cout << pArray[index] << endl;
     }
     
}
-------- "Do you believe in Ghost?"

Share this post


Link to post
Share on other sites
Advertisement
Is this a compile time error or a runtime error? I''m not going to look through your code unless I have at least some idea what your problem is.

Share this post


Link to post
Share on other sites
i could be wrong, (which i probably am, buuut)...isn' tthe syntax for declaring a template thus:


template <class type>
void F(type t)
{
t = t;
}


notice the "class" keyword...
am i missing something? otherwise you haven't declared "typename" anywhere...

------------------------------------------
Post Reply

[edited by - MrBeaner on February 7, 2004 12:52:27 AM]

Share this post


Link to post
Share on other sites
Both typename and class are legit, unless your template parameter is itself a template.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
All template parameters must be compile-time constants... that means you cannot do:

Process<int, array.GetSize(), int(rand() % 10)>(array);


since "int(rand() % 10" is not a compile-time constant.

Share this post


Link to post
Share on other sites
Its a compile error on the line Daniel just pointed out.

What do you mean by compile-constant?

--------
"Do you believe in Ghost?"

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
Both typename and class are legit, unless your template parameter is itself a template.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)



What is the difference between the two?

Share this post


Link to post
Share on other sites
MrBeaner: There is no difference. [/matrix reference] Seriously. class and typename are interpreted the same way in template parameter lists.

Share this post


Link to post
Share on other sites
quote:
Original post by MrBeaner
What is the difference between the two?


Within a template declaration, they are completely interchangeable.
Only when you have something of the form template <template<typename T> class U> ... is the 'outer' class mandatory (because U really is a class).

The typename keyword has other usages, all related to the need to disambiguates between members which are variables/functions... and members which are types:

template <class Container>
typename T::value_type
first(Container c)
{
return *(c.begin());
}


Without the typename keyword, your compiler would interpret T::value_type as a static member. typename precises that it really is a type. Since T is not yet known when you write the template, you have to specify which one is right.

[edited by - Fruny on February 8, 2004 1:51:09 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Leiarchy9
Its a compile error on the line Daniel just pointed out.

What do you mean by compile-constant?

--------
"Do you believe in Ghost?"




A compile constant is a constant which value is known at compile-time...
For example:

int b;
ExampleClass<2+4*3> a; //You can do this, since 2+4*3 is a constant that is known at compile-time

ExampleClass<3*b> c; //This cause an error, since you don`t know at compile time what will be the value of "b" by the time this line is executed in runtime

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!