template trouble, help understand


long winded i think, but correct any mis-conceptions please, and repost.
I'm really new and thought of this, but can't get a compile to complete.
getting the following the following 2 errors on the sole 2 lines in main inside the 2 for loops.


delete_cls_array.h|44|error: invalid types 'examp<long double>*[long double]' for array subscript|

I appreciate any help in advance.
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
48
template <class T>class BaseExamp {                    //Base undefined type template object
  T i,j;                                               //create type T vars of i, j
public:
     void init(T a,T b){                               //used in convience constructor
          i=a,j=b;                                     //assign a and b
     }
     BaseExamp(){}
     BaseExamp(T a,T b){ init(a, b);}                  //convience constructor
     ~BaseExamp(){}                                    //i and j removes from memory
     T product()  {                                    //definition of individual
          return i*j;                                  //get(s) and set(s) for i and j
     }                                                 //would follow too
};
template <class T>class examp : public BaseExamp<T> {  //inherits from Base with type T as well, right?
  T* a,b;                                              //In the real world this would be a 
public:                                                //    reference counting smart pointer of the actual T, right?
     void init(T &ii,T &jj){                           //these values re-assign by and with ref(s)
          &a=ii,&b=jj;                                 //right?
     }
     examp(){}                                         //std ctr
     examp(T &ii,T &jj){ init(ii, jj);}                //conv. ctr
     ~examp(){}
     T product()  {                                    //ensure properness with
          return ((&a)*&b);                            //these exact parentheses 
     }                                                 //right?
};

int main() {
  examp <long double>*ptr;                           //create a pointer of type examp
  long double i;                                     //just a tmp loop var
  ptr = new examp<long double>[6];                   //6 pointers = 6 objects allocatted
  if(!ptr) {                            //IMPORTANT: Always do this check for all poniters 
    printf("Allocation error.\n");        //           (inside it's wrapped smart pointer, not shown here)
       return 1;                          //  before terminattion, or the try something else function...
  }
  for(i=0;i<6;i++) {                                 //assigns i and j for all ptr[i](s)
int xx;
     xx = i;
     ptr[i].init(xx,xx);                             //[0]=0,0; [1]=1,1;...[4]=4,4; [5]=5,5;
  }
  for(i=0;i<6;i++) {                    
     printf("Product[%i]is:%f\n", i, ptr[i].product());//std printf call
  }
  delete[] ptr;                                         //proper deletion of ptr with no smart pointer
  return 0;                                             //right?
}



PS:
what i am aiming to do is have pointers act like the memeber of the super class
interacting with the pointer interacts with values in the super class
i don't know if this is even the right way,
but what i really want is:
a object that changes a member's datatype on a function call
perhaps achieved by the usages of pointers
i (the i in main, not me) should be an integer.

Though, I'm not sure I understand what you're trying to do... Could you elaborate?
Last edited on
i'm hoping these objects could:
Examp<short int> si; is declared
Examp<long double> is now needed instead
the type is stored in BaseExamp (the member data)
a pointer is stored in Examp,
can the reference of the a, and b of Examp point to a different data type?
if not, could the BaseExamp have it's data type changed?
further if not, could a Examp<short int> replace it self with
Examp<long double>?

if so for any or all, demonstrate a way please

I kinda have decent hold on terminology, feel free to speak pseudo code...

further appreciation for any more help...thx
There are ways you can specify/modify an object's behaviour dynamically
(abstract factory pattern, strategy pattern, ...), which can be used to also
specify/modify an object's data type dynamically.

But why do you want to do this? Could you describe the problem in english?
So far, you've described the way you're trying to implement what you want to do,
but you haven't described what you want to do. It's more likely that you'll get a good reply
if you elaborate on the problem itself rather than on your approach to solve it.
long double i;

should be replaced by

int i;

This mistake has nothing to do with templates: in c++ you can't index arrays by doubles (do you come from another programming language?).

[Edit:] Just noticed your templates have some other mistakes:

line 39:
(ptr[i]).init(xx, xx); is illegal as xx is not of type examp <long double>
Last edited on
okay, the above code does not do what i want to do, i mis-understood c++ templates,
we can only define a template class to be type independent, where as in the the use of the template, the type is dependent, must be defined.

What i want to do is:
a class that holds a undefined T taking 1 parameter
if the numerical value (some value like 37) were passed into this object,
the T is designated byte, where if the value 8.1 billion were put in,
the T is designated long double, achieving this for the other remaining numeric types too.

Is this the right way?
if not, what is the right way?
the important aspect is the T is always the smallest appropriate data type.
An object in idea would hold a class Object<class T> object for the accessing of
the created object.

Ok, so you want a variable that can hold data of any type. That's easy.

Check out boost::any ( http://www.boost.org/doc/libs/1_46_1/doc/html/any.html )
and boost::variant ( http://www.boost.org/doc/libs/1_46_1/doc/html/variant.html )

If you prefer homemade solutions...

One way to do it is to use type erasure ( http://www.cplusplus.com/forum/articles/18756/ )

Another way would be something like this:

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
class Variable
{
private:

    enum {INT, DOUBLE, STRING};

    union
    {
        int i;
        double d;
        std::string * s;
    } data;

    int type;

public:

    Variable():type(INT) {data.i=0;}
    Variable(const Variable & var) {/*...*/}
    Variable & operator=(const Variable * var) {/*...*/}
    ~Variable() {if (type==STRING) delete data.s;}

    void Set(int value)
    {
        if (type==STRING) delete data.s;

        type=INT;
        data.i=value;
    }

    void Set(double value) {/*...*/}
    void Set(const std::string & value) {/*...*/}

    void Get(int & value)
    {
        if (type!=INT)  { cerr << "error!\n"; return; }

        value=data.i;
    }

    void Get(double & value) {/*...*/}
    void Get(std::string & value) {/*...*/}
};


thank you

i see now

i must say really helpful

I appreciate it

thx
Topic archived. No new replies allowed.