Templates and pointers

This is a discussion on Templates and pointers within the C++ Programming forums, part of the General Programming Boards category; Code: // POINT_BUBSORT.CPP // Bubble sort function. Sorts an array of ints in descending order. //template<class element> void pointerBubbleSort(int *array, ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Templates and pointers

    Code:
    // POINT_BUBSORT.CPP
    // Bubble sort function. Sorts an array of ints in descending order.
    //template<class element>
    void pointerBubbleSort(int *array, int n)
    {
      int *pointer = array;
      int i, j, flag = 1;
      int temp;
    
      for(i = 1; (i <= n) && flag; i++)
       {
         flag = 0;
         for(j = 0; j < (n - i); j++)
    	{
    	  if (pointer[j + 1] < pointer[j])
    	   {
    	     temp = pointer[j + 1];
    	     pointer[j + 1] = pointer[j];
    	     pointer[j] = temp;
    	     flag = 1;
    	   }
    	}
    //	 if (i%1000 == 0)
    //		 cout << i << endl;
        }
    }
    I want to use the apvector class with it, but I don't know how to use a template of pointers...

    Code:
    void pointerBubbleSort(int *array, int n)
    {
      int *pointer = array;
    versus

    Code:
    template<class element>
    void apvPointerBubbleSort(apvector <element*> array, int n)
    {
      apvector <element> *pointer = array;
    How do I do the second one so that it will work?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you should probably pass it in by reference so that working with the vector will be a little nicer....

    void apvPointerBubbleSort(apvector<element> & array)
    {

    and there doesn't seem to be any need for the assignment you do on the first line although you can do that as a reference also if you want.

    apvector<element> & alias = array;
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I need it to work the way it is because it uses pointers to the apstring.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: Templates and pointers

    well I'm not sure I'm totally following you
    Originally posted by Trauts
    Code:
    template<class element>
    void apvPointerBubbleSort(apvector <element*> array, int n)
    {
      apvector <element> *pointer = array;
    This is bad because apvector<element*> is a different type than apvector <element> That's problem number one that I see. If you are trying to pass in a pointer to a apvector<element> then you do it like this...

    (apvector<element> *array, int n);

    ok?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I tried that yesterday, and it gave me an error:

    Code:
    error C2784: 'void __cdecl apvPointerBubbleSort(class apvector<element> *,int)' : could not deduce template argument for 'class apvector<itemType> *' from 'class apvector<int>'

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Red face

    You get those problems when you use non standard C++ function such as apstring/apvector.
    Mr. C: Author and Instructor

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    template<class element>
    void apvPointerBubbleSort(apvector <element> &array, int n)
    {
        ..........
    }
    
    ..........
    apvector <apstring *> strings(2);
    strings[0]=new apstring("hey");
    strings[1]=new apstring("boo");
    apvPointerBubbleSort(strings, strings.length());
    // delete them here....

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Its not using apstrings as a data type all of the time.
    It'll be using numbers, too.

    Eh, disregard that ^ I just figured out what you meant.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue with templates
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2008, 06:06 AM
  2. Object pointers and Templates
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2004, 08:41 AM
  3. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 11:39 AM
  4. templates with pointers
    By Cipher in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 10:45 AM
  5. tracing pointers
    By makimura in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2001, 08:55 AM

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