Results 1 to 2 of 2

Thread: Template Question, why doesnt this work?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    Template Question, why doesnt this work?

    For some reason the template
    void swap(T *const element1Ptr, T *const element2Ptr)
    doesnt work, anyone know why?.

    Heres the code:

    //Jason Project 3.
    //This program puts values into an array, sorts the values into
    //ascending order, and prints the resulting array using the template function bubbleSort.
    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    #include<fstream>

    using namespace std;

    //Template to Sort Arrays
    template<class T>
    void bubbleSort(T *array, const int size)
    {
    void swap(T *const, T *const);
    for(int pass = 0;pass < size - 1;pass++)
    for(int j = 0;j < size - 1;j++)
    if(array[j] > array[j+1])
    swap(&array[j], &array[j+1]);
    }

    template<class T>
    void swap(T *const element1Ptr, T *const element2Ptr)
    {
    //Function swaps values of two variables for use with bubble sort.
    T hold = *element1Ptr;
    *element1Ptr = *element2Ptr;
    *element2Ptr = hold;
    }


    //Template to set Array Values.
    template<class T>
    void setarray(T *array, const int size)
    {
    for(int i = 0;i < size;i++)
    array[i] = rand() & 999;
    }

    //Function Definitions
    int getseed();


    int main()
    {
    ofstream out("C:\\Driver.txt", ios:ut);
    const int arraySize = 100;
    int i;
    int a[arraySize];
    float b[arraySize];
    srand(getseed());

    //Sets Values Into The Array.
    setarray(a, arraySize);
    setarray(b, arraySize);

    //Outputs Heading For File.
    out<< "Jason , Project 3, April 29, 2002"<< endl;

    //Outputs integer Array Before and After Sorting.
    out<< "Integer Array in original order\n";
    for(i=0;i<arraySize;i++)
    out<< setw(4)<<a[i];
    bubbleSort(a, arraySize); //Sort the array.
    out<< endl<< "Integer Array After Sorting"<< endl;
    for(i=0;i<arraySize;i++)
    out<<setw(4)<<a[i];
    out<< endl;

    //Outputs float Array Before and After Sorting.
    out<< "Float Array in original order"<< endl;
    for(i=0;i < arraySize;i++)
    out<< setw(4)<<b[i];
    bubbleSort(b, arraySize); //Sort the array.
    out<< endl<< "Float Array After Sorting"<< endl;
    for(i=0;i < arraySize; i++)
    out<< setw(4)<< b[i];
    out<< endl;
    return 0;
    }

    int getseed()
    {
    //Function returns random value based on time and date.
    //Used to return a better random value.
    char temp;
    int len, pos, seed;
    string bob;

    //Gets System Time
    char tmp[80];
    struct tm *ptr;
    time_t lt;
    lt = time(NULL);
    ptr = localtime(&lt);
    bob = asctime(ptr);

    len = bob.length();
    for(pos=0;pos<len;pos++){
    temp = bob.at(pos);
    if(isdigit(temp))
    seed = seed + int(temp);}
    return seed;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Have you tried removing the declaration of swap that is within the bubbleSort function and moving the swap definition to the top of the file?

    Do you get compile errors or runtime errors?
    Which compile errors?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width