Thread: I still need help on this!

  1. #1
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    I still need help on this!

    How do I initialize my templated array class??

    So let's say my template class is this...

    template <class T>
    class RArray {
    private:
    typedef enum { StartArraySize = 128} bogus;
    typedef enum {InitialSize = 0};

    size_t num_elem;

    size_t array_size;

    size_t array_Cursor;

    T *pArray;


    private:

    public:


    I can add an init routine like this?


    RArray()
    {
    pArray = new T[ StartArraySize ];
    num_elem = InitialSize;
    array_Cursor = InitialSize;
    array_size = StartArraySize;
    }


    Like this?

    void Init()
    {
    int i = 0;
    for (;i< StartArraySize;i++)
    {
    pArray[i] = 0;
    }
    pArray
    }



    ~RArray()
    {
    if (pArray != NULL) {
    delete [] pArray;
    }
    }
    Last edited by adonisv; 02-19-2003 at 06:23 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    code tags, and more descriptive title.

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    pretend i have this..(example)

    Code:
    template<class t>
    class test
    {
    public:
    test();
    int somefunction();
    };
    
    // to define it outside the class you need to do this
    template<class t>
    test<t>::test()
    {
    //default constructor..whatever you want!
    }
    
    template<class t>
    int test<t>::somefunction()
    {
    //still whatever you want
    }
    hope that helps
    nextus, the samurai warrior

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    also did you know you can do this?

    Code:
    template<class t, int num> //<---look at that
    class test
    .
    .
    .
    .
    //in the constructor
    template<class t, int num>
    test<t, num>::test()
    {
    pArray = new t[num]; // the person can make array any size they want
    ...
    ..
    }
    
    int main()
    {
    test<int, 20> array; //wow array of 20
    
    return 0; // very famous
    }
    nextus, the samurai warrior

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    also your class definition is just like a "blue print" so you can not initialize anything in the class, but you can assign it stuff in the constructor etc..or other functions...instead of using

    Code:
    void Init()
    put the body of that function in the constructor...much better
    nextus, the samurai warrior

  6. #6
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Unhappy

    So I can't have an initializaton routine IN the class? I'm so confused....

  7. #7
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    you sorta can..its in the constructors..look

    you cant do this
    Code:
    class test
    {
    private:
    int number = 5;
    };
    nope wrong!

    but you can do this
    Code:
    class test
    {
    private:
    int number;
    public:
    test(int x = 5): number(x) {}
    };
    
    // or
    class test
    {
    private:
    int number;
    public:
    test(int x = 5)
    {
    number = x;
    }
    };
    now you get it?
    nextus, the samurai warrior

  8. #8
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Cool

    I guess what confuses me is that since my template is generic and is supposed to take any "type" that I pass it, how would I set all of its initial elements to NULL or zero or whatever? Is this even a good idea for a template since I will be overwriting the entries eventually anyway?
    Logic, Reason and Creativity...

  9. #9
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    the almighty beautiful "for" loop and set it equal to 0;
    and i think NULL is another keyword for 0 (really means same i think...i read it somewhere in my book or so)

    so
    Code:
    int* pnum = NULL;
    int* pnum2 = 0; //both points to nothing
    hehe..hope that helps
    nextus, the samurai warrior

  10. #10
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Question

    RArray()
    {
    int i = 0;
    num_elem = num;
    array_Cursor = sizeof(0);
    pArray = new T[num];

    for(;i<num;i++)
    {
    pArray[i] = array_Cursor;
    //also with 0 or NULL
    }

    }
    My compiler says the equal operator is ambiguous...


    --------------------Configuration: ADOXperiment - Win32 Debug--------------------
    Compiling...
    ADOXperiment.cpp
    d:\data\vss\coderesearch\adoxperiment\debug\msadox .tlh(1201) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
    d:\data\vss\coderesearch\adoxperiment\debug\msado1 5.tlh(409) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
    d:\data\vss\coderesearch\adoxperiment\rarray.h(85) : error C2593: 'operator =' is ambiguous
    d:\data\vss\coderesearch\adoxperiment\rarray.h(75) : while compiling class-template member function '__thiscall RArray<class _variant_t,100>::RArray<class _variant_t,100>(void)'
    Error executing cl.exe.

    ADOXperiment.exe - 1 error(s), 2 warning(s)
    Logic, Reason and Creativity...

  11. #11
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    what is num equal to? your didnt put anything in your constructor to take values

    RArray()

    .....think of the constructor as a regular function
    also..pArray[i] = 0; // good enough dont use sizeof(0)
    nextus, the samurai warrior

  12. #12
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14
    I don't need to with this syntax my code is smaller

    template <class T, int num>
    class RArray {
    ....

    I called it this way

    // Create instance of the templated array class. Establish that
    // it will be holding _variant_t data types.

    RArray< _variant_t,100 > BookMarks;

    Compiler says ok and it runs....
    Logic, Reason and Creativity...

  13. #13
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Exclamation Compiler no likey likey...

    RArray()
    {
    int i = 0;
    num_elem = num;
    array_Cursor = sizeof(0);
    pArray = new T[num];

    for(;i<num;i++)
    {
    pArray[i] = 0;
    }

    }

    Logic, Reason and Creativity...

  14. #14
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    well what value you get for num..did you past it into the RArray(int num) or was it defined in your class

  15. #15
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14
    I passed it a "num" when I declared it in the function where it's used..

    int ArchiveTable(const char* copiedTableName,const char* alsoCopiedTable,const char* dataSource)
    {
    // Return negative one if table name and datsource are null.
    if (!copiedTableName || !dataSource || alsoCopiedTable)
    return -1;

    // Declare and initialize local variables.
    bool bSuccessful = false;
    enum DataTypeEnum columnType = adDate;
    long columnCount = 0;


    // Define ADOX variable types.
    HRESULT hr = S_OK;
    _bstr_t columnName;
    _variant_t vIndex;
    _variant_t vNull;



    // Create instance of the templated array class. Establish that
    // it will be holding _variant_t data types.

    .... RArray< _variant_t,100 > BookMarks;

    other fun stuff
    Logic, Reason and Creativity...

Popular pages Recent additions subscribe to a feed