User-specified datatypes

I have a template Array class that should create objects of ints, chars, and doubles.

How could I write my main.cpp file so that the user can define the datatype of the array?

For example, I cout to the user: "What type of array do you want?" And then, based on his response (which will either be char, int or double), I instantiate an object of that type.

It seems simple enough, but my code won't compile when I do it!
Here's what my main looks like:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

#include "Array.h"

template<typename T> T Array<T>:: dvalue = 'X';

main()
{
        //The chief purpose of this file is to stress and test the .h file

        int size = 0;
        string yes_no;
        string cyi;
        int bad_input_ctr = 0;

        do
        {
                if(bad_input >= 1)
                        cout << "Please type either the word 'char' for char, 'int' for int, or 'double' for double\n";
                else
                        cout << "Do you want the array to be a char, int, or double? (char/int/double)?\n";
                cin >> cyi;
                bad_input_ctr++;
        } while(cyi != "char" && cyi != "int" && cyi != "double");

        if(cyi == "char")
        {
                Array<char> arr1(size);
                Array<char> arr2;
        }
        else
                if(cyi == "int")
                {
                        Array<int> arr1(size);
                        Array<int> arr2;
                }
                else
                {
                        Array<double> arr1(size);
                        Array<double> arr2;
                }

        cout << "Enter the values of the array\n";

        cin >> arr1;
}


All the other functions in my .h work with each datatype. This is the only problem.
Last edited on
I made some edits to the original post in case it wasn't clear what I was asking.

Any ideas?
Last edited on
There is no variable in scope with the name arr1 at line 46. The variables that you create on line 29-30 will go out of scope on line 31. Same problem with with the variables on lines 35-36 and 40-41.

Array<int>, Array<double> and Array<char> are three completely different types. You can't have a variable have different types.
Then I'm stuck.

If I can't define Array<int> , Array<double> , and Array<char> separately, then how could I ever possibly instantiate on object of a type specified by the user? I don't know what kind of data type the user will want until run time.....

Well, you might be able to solve it using inheritance and virtual functions. This is the usual way to solve the problem of having the type be decided at runtime.
Templates have to know the type at compile time. This isn't Java generics (which are a mess anyway). The compiler will compile a copy of your template for each type that will be using it.
Topic archived. No new replies allowed.