//Hi Lads.This is my first attempt to create template functions.
 //Here is the main ,which needed to create Locate.h file/header and 
 //in that Locate.h create  templates to support the main.
 //If the value does not exist in the array, -1 is returned.
 // Templates.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Locate.h"
using namespace std;

typedef int ItemType;

int _tmain(int argc, _TCHAR* argv[])
{
    const int arraySize = 10;
    ItemType array1[arraySize] = { 23, 43, 12, 45, 65, 77, 22, 10, 99, 43 };

    cout << "The values in the array as are follows: \n";

    display_array(array1, arraySize);

    ItemType target;
    cout << "Enter a Value currently in the Array: ";
    cin >> target;

    int result = arrayLocation(array1, arraySize, target);

    if (result != -1)
        cout << "\n\nThe Value " << target << " was found at location "
        << result << endl;
    else
        cout << "\n\nThe Value " << target << " was NOT found in the array" << endl;
    return 0;
}


//And here,my attempt to do it
//Please help me to identify the problem,i now from compiling i have 5-6 errors

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here
#include<iostream>

template<class T >
 T  display_array(T ItemType[10] , T const int arraySize)


{

    cout << array1 << ' ' << arraySize;


}

 const int arraySize = 10;
 template<class T>


 streuct Item Type{
     int length;
     int array1[arraySize];


 }





template<class T>
T   arrayLocation(  T ItemType[10] , T const int , T Item Type)
{
    int first = 0;
    int last = array1.length - 1;
    int midPoint;
    found = false;
    while (first < last)
    {
        midPoint = (first + last) / 2;
        if (target > array1.ItemType array1[midPoint])
            first = midPoint + 1;
        else
            last = midPoint;
    }
        if (last == -1)
            return -1;
        else if (target == ItemType array[first])
        {
            found = true;
            return first;


        }
        else
            return -1;
}

Line 54

template<class T >
T  display_array(T ItemType[10] , T const int arraySize)

This is invalid. Remeber that the compiler will substitude the type T in the template into the code provided in order to instatiate the function for you. You can do this by hand and should get the valid code. In this code if T is int (as is the case in main) then the code becomes

int  display_array(int ItemType[10] , int const int arraySize)

Hopefully it is obvious that the type of the 2nd parameter int const int is not valid. It should presumably be const int which would be removing the T from the beginning of the parameter type.

Line 59

cout << array1 << ' ' << arraySize;

array1 is not visible here, it is a local variable to main. Even if you changed it to ItemType, which is a valid local variable, you are just going to get an address (because ItemType is a pointer) or alternitively a compiler warning/error.

Line 66

 template<class T>


 streuct Item Type{
     int length;
     int array1[arraySize];
 }

You appear to be trying to declare struct ItemType, ignoring spelling an spaces you appear to be confusing type names and variable names. In various places in this code you use ItemType as a type name (here and top of main) or a variable name template at line 54. Also you have declared this structure as a template dependent on type T and then haven't used the templated type T anywhere in the definition of ItemType. I suspect you are just trying to declare a normal structure in which case template<class T> should not be there.

Line 79

template<class T>
T   arrayLocation(  T ItemType[10] , T const int , T Item Type)

Has the same int const int problem and again a space in Item Type

Line 89

if (target > array1.ItemType array1[midPoint])

I'm actually not sure what you are trying to do here. You have tried to access array1 again, which is inaccessable since it is local to main and then used it as though it where a structure not an array.

I suggest that you write the code as non-templated functions that use type int first. Then when you get that working covert it to use a generalised type T instead of int. If you do that remember that your size parameters will continue to be int not the generalised type.

Thanks for pointing to my misstakes.The whole idea is to make Locate.h with templates for the main.How will i start.I have seen examples,but then try to make in reverse(then main done ,make template)i a bit confused here.I just need to see it once in action,how to correct implement templates,as i have 2 functions.Regards

Like I said if you are having trouble with templates then start by working out how a normal function would work, then templatise it.

So a function to print all entries in an array of any type given a pointer to the array and it's size, i.e. display_array becomes a function to print all entries in an array of int.

That is not too hard it is something like

void display_array( const int array[], const int size)
{
    for(int ix=0; ix < size; ix++)
    {
      std::cout << array[ix] << std::endl;
    }
}

Now templateise it, the array can be of any type so int array becomes T array, not much else changes; we assume that ostreamn::operator<< is defined for type T.

template<typename T>
void display_array( const T array[], const int size)
{
    for(int ix=0; ix < size; ix++)
    {
      std::cout << array[ix] << std::endl;
    }
}

I have simply changed those types that are to be part of the template definition to use the template parameter. I leave arrayLocation as an exercise for you to do yourself noting it probably relies on there being an operator== (and operator>) for the type used in the template.

Once you have created your template functions then you can initially test them using int types which should produce the same results as your original non-templated code and then you can define your alternate types (structure) that you wish to use.

Thank you so much for answers. I just was confused were to put T or not,like i see array can be any type,but size of array is int,so we do not tampalte it.Because as i asumed if i declare T,need to assign it to every declaration,put everywere,but looks like not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.