×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    45

    ARMI Template error

    Hi , I'm experiencing a problem with a template class I've made to add a simple object collection to my app. The problem is that on WINS build it build whithout errors but on ARMI build I get a parsing error.
    Can someone more experienced point me to what the problem may be. Here is the code:
    -I have the template and a specialization for HBufC

    -----Collection.h----------

    #ifndef _COLLECTION_H_
    #define _COLLECTION_H_

    #include <e32base.h>


    template <class K, class T>
    class CCollection
    {

    struct Item
    {
    K key;
    T value;
    };
    public:

    static CCollection* NewL();
    static CCollection* NewLC();
    ~CCollection();
    public:

    void AppendL(K key, T value);
    TInt Find(K key);
    T& Get(K key);
    T& GetByIndex(TInt index);
    K& GetKeyByIndex(TInt index);
    void RemoveByIndex(TInt index);
    void Remove(K key);
    TInt Count();
    private:
    CCollection();
    void ConstructL();


    private:
    CArrayPtr<Item>* iCollection;
    };

    template<class K, class T>
    CCollection<K, T>* CCollection<K, T>::NewL()
    {
    CCollection<K, T>* self = CCollection<K, T>::NewLC();
    CleanupStack::Pop(self);
    return self;
    }

    template<class K, class T>
    CCollection<K, T>* CCollection<K, T>::NewLC()
    {
    CCollection<K, T>* self = new (ELeave) CCollection<K, T>();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }


    template<class K, class T>
    CCollection<K, T>::CCollection()
    {

    }

    template<class K, class T>
    void CCollection<K, T>::ConstructL()
    {
    iCollection = new (ELeave) CArrayPtrFlat<Item>(1);
    }

    template<class K, class T>
    void CCollection<K, T>::AppendL(K key, T value)
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    if (key == (iCollection->At(i))->key)
    {
    RemoveByIndex(i);
    break;
    }
    }

    Item* it = new (ELeave) Item;
    it->key = key;
    it->value = value;
    iCollection->AppendL(it, 1);
    }

    template<class K, class T>
    TInt CCollection<K, T>::Find(K key)
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    if (key == (iCollection->At(i))->key)
    {
    return i;
    }
    }

    return 0;
    }

    template<class K, class T>
    T& CCollection<K, T>::Get(K key)
    {
    return GetByIndex(Find(K));
    }

    template<class K, class T>
    T& CCollection<K, T>::GetByIndex(TInt index)
    {
    __ASSERT_ALWAYS(index < iCollection->Count(), User::Panic(_L("<CColection>"), -1));
    return (iCollection->At(index))->value;
    }

    template<class K, class T>
    K& CCollection<K, T>::GetKeyByIndex(TInt index)
    {
    __ASSERT_ALWAYS(index < iCollection->Count(), User::Panic(_L("<CColection>"), -1));
    return (iCollection->At(index))->key;
    }

    template<class K, class T>
    void CCollection<K, T>::RemoveByIndex(TInt index)
    {
    if (index < iCollection->Count())
    {
    delete (iCollection->At(index));
    iCollection->Delete(index);
    }

    }

    template<class K, class T>
    void CCollection<K, T>::Remove(K key)
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    if (key == (iCollection->At(i))->key)
    {
    RemoveByIndex(i);
    }
    }
    }

    template<class K, class T>
    CCollection<K, T>::~CCollection()
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    RemoveByIndex(i);
    }
    delete iCollection;
    }

    template<class K, class T>
    TInt CCollection<K, T>::Count()
    {
    return iCollection->Count();
    }
    #endif // _COLLECTION_H_

    ---------Collectionspec.h-----------
    #ifndef _COLLECTIONSPEC_H_
    #define _COLLECTIONSPEC_H_

    #include "collection.h"

    template<>
    class CCollection<HBufC*, HBufC*>
    {
    struct Item
    {
    HBufC* key;
    HBufC* value;
    };
    public:
    static CCollection* NewL();
    static CCollection* NewLC();
    ~CCollection();

    public:
    void AppendL(HBufC* key, HBufC* value);
    TInt Find(HBufC* key);
    HBufC* Get(HBufC* key);
    HBufC* GetByIndex(TInt index);
    HBufC* GetKeyByIndex(TInt index);
    void RemoveByIndex(TInt index);
    void Remove(HBufC* key);
    protected:
    private:

    CCollection();
    void ConstructL();

    private:
    CArrayPtr<Item>* iCollection;
    };

    CCollection<HBufC*, HBufC*>* CCollection<HBufC*, HBufC*>::NewL()
    {
    CCollection<HBufC*, HBufC*>* self = CCollection<HBufC*, HBufC*>::NewLC();
    CleanupStack::Pop(self);
    return self;
    }

    CCollection<HBufC*, HBufC*>* CCollection<HBufC*, HBufC*>::NewLC()
    {
    CCollection<HBufC*, HBufC*>* self = new (ELeave) CCollection<HBufC*, HBufC*>();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }


    CCollection<HBufC*, HBufC*>::CCollection()
    {

    }

    void CCollection<HBufC*, HBufC*>::ConstructL()
    {
    iCollection = new (ELeave) CArrayPtrFlat<Item>(1);
    }

    void CCollection<HBufC*, HBufC*>::AppendL(HBufC* key, HBufC* value)
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    if (*key == *((iCollection->At(i))->key))
    {
    RemoveByIndex(i);
    break;
    }
    }

    Item* it = new (ELeave) Item;
    it->key = key;
    it->value = value;
    iCollection->AppendL(it, 1);
    }

    TInt CCollection<HBufC*, HBufC*>::Find(HBufC* key)
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    if (*key == *((iCollection->At(i))->key))
    {
    return i;
    }
    }

    return 0;
    }

    void CCollection<HBufC*, HBufC*>::RemoveByIndex(TInt index)
    {
    if (index < iCollection->Count())
    {
    delete (iCollection->At(index))->key;
    delete (iCollection->At(index))->value;
    delete (iCollection->At(index));
    iCollection->Delete(index);
    }

    }

    void CCollection<HBufC*, HBufC*>::Remove(HBufC* key)
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    if (*key == *((iCollection->At(i))->key))
    {
    RemoveByIndex(i);
    }
    }
    }

    CCollection<HBufC*, HBufC*>::~CCollection()
    {
    for (TInt i = 0; i < iCollection->Count(); ++i)
    {
    RemoveByIndex(i);
    }
    delete iCollection;
    }

    TInt CCollection<HBufC*, HBufC*>::Count()
    {
    return iCollection->Count();
    }

    HBufC* CCollection<HBufC*, HBufC*>::Get(HBufC* key)
    {
    return GetByIndex(Find(HBufC*));
    }

    HBufC* CCollection<HBufC*, HBufC*>::GetByIndex(TInt index)
    {
    __ASSERT_ALWAYS(index < iCollection->Count(), User::Panic(_L("<CColection>"), -1));
    return (iCollection->At(index))->value;
    }

    HBufC* CCollection<HBufC*, HBufC*>::GetKeyByIndex(TInt index)
    {
    __ASSERT_ALWAYS(index < iCollection->Count(), User::Panic(_L("<CColection>"), -1));
    return (iCollection->At(index))->key;
    }

    #endif // _COLLECTIONSPEC_H_

  2. #2
    Regular Contributor
    Join Date
    May 2004
    Location
    Melbourne, Australia
    Posts
    75

    Re: ARMI Template error

    Hello there!

    It would be useful if you also posted exactly what the compiler is complaining about... In any case whilst looking at your code, i found this:

    template<class K, class T>
    T& CCollection<K, T>::Get(K key)
    {
    return GetByIndex(Find(K));
    }

    You are passing type K to your Find function, that doesnt make any sense. Shouldnt it be key? Also sounds really strange that the wins compiler didnt complain about it.

    Hope that helps!
    cheers

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    45

    Re: ARMI Template error

    Yes, indeed, it seems that I wasn't carefull enough. The key should have been passed. Thanks a lot. Though it seems very strange Visual Studio didn't complained about it. Anyway thanks again

Similar Threads

  1. XMl Parser Implementation for Symbian
    By ittiam in forum Symbian
    Replies: 26
    Last Post: 2009-09-16, 05:26
  2. problem with Code Warrior 5.2
    By mayankkedia in forum Symbian
    Replies: 4
    Last Post: 2006-06-22, 08:26
  3. code warrior problems
    By mayankkedia in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 2
    Last Post: 2004-10-25, 06:59
  4. problem with codewarrior 5.2
    By mayankkedia in forum Symbian Tools & SDKs
    Replies: 3
    Last Post: 2004-09-27, 16:08
  5. Cannot compile SDK's examples for 7650 platform...
    By midletsoft in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2003-08-12, 19:11

Posting Permissions

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