[Bada] Creating a LinkedListT or ArrayListT to Hold Your Own Type of Objects

Recently, I had been trying to use make use of the ArrayListT and the LinkedListT and it took me a few attempts to figure out how to use it. Unlike ArrayList and LinkedList, ArrayListT and LinkedListT supports generics, so you that you can add a specific type of object and get back that object as the specific type without having to cast it from Object. If you want to use them your own type of objects, there are few things to know otherwise it will not compile. In my case, I was using the trying to create a list of Entry objects. Originally, it looked something like this:

class Entry
{
private:
    String _pFirstName;
    String _pLastName;

public:
    Entry(const String &firstName, const String &lastName);
    const String& GetFirstName();
    const String& GetLastName();

    virtual ~Entry();
};

In my initial attempt to compile, this is part of the output:

D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::IndexOf(const Type&, int, int, int&) const [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here
D:/Bada/1.0.0/include/FBaseColLinkedListT.h:386: error: no match for 'operator==' in 'obj == pNode->Osp::Base::Collection::__LinkedListNodeT<Entry>::pObj'
D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::LastIndexOf(const Type&, int&) const [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here
D:/Bada/1.0.0/include/FBaseColLinkedListT.h:542: error: no match for 'operator==' in 'obj == pNode->Osp::Base::Collection::__LinkedListNodeT<Entry>::pObj'
D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::InsertItemsFrom(const Osp::Base::Collection::ICollectionT<Type>&, int) [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here

In case you do not understand the above output, it is complaining that the “==” operator had not been defined in my Entry class (the “data” that I was trying to store). As it turned out, I had missed the following parts in the documentation for LinkedListT and ArrayListT:

Several methods in the LinkedListT class need the following operators of type: assignment(=), equivalent(==), and not equivalent(!=). Furthermore, to use the Sort() method, relational operators(&ltj;, >) or a comparer must be provided.

and …

Several methods in ArrayListT class need the following operators of Type: assignment(=), equivalent(==), and not equivalent(!=). Furthermore, to use Sort() methods, relational operators() or comparer must be provided.

So, then I added the following to my Entry class:

    bool operator==(const Entry& object);
    bool operator!=(const Entry& object);
    Entry& operator=(const Entry& rhs);

However, when I compiled again, I was still getting errors:

D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::IndexOf(const Type&, int, int, int&) const [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here
D:/Bada/1.0.0/include/FBaseColLinkedListT.h:386: error: passing 'const Entry' as 'this' argument of 'bool Entry::operator==(const Entry&)' discards qualifiers
D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::LastIndexOf(const Type&, int&) const [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here
D:/Bada/1.0.0/include/FBaseColLinkedListT.h:542: error: passing 'const Entry' as 'this' argument of 'bool Entry::operator==(const Entry&)' discards qualifiers
D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::InsertItemsFrom(const Osp::Base::Collection::ICollectionT<Type>&, int) [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here

Basically, the equivalent and not equivalent operators needed to be delcared as functions that does not modify Entry (or at least does not change the value of the member variables). The operators became:

    bool operator==(const Entry& object) const;
    bool operator!=(const Entry& object) const;
    Entry& operator=(const Entry& rhs);

However, there were still problems when I tried to compile it:

D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::InsertItemsFrom(const Osp::Base::Collection::ICollectionT<Type>&, int) [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here
D:/Bada/1.0.0/include/FBaseColLinkedListT.h:485: error: no matching function for call to 'Entry::Entry()'
D:/Workspace/Bada/Experimental/TypedListDemo/inc/Entry.h:26: note: candidates are: Entry::Entry(const Osp::Base::String&, const Osp::Base::String&)
D:/Workspace/Bada/Experimental/TypedListDemo/inc/Entry.h:15: note:                 Entry::Entry(const Entry&)
D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::RemoveItems(const Osp::Base::Collection::ICollectionT<Type>&) [with Type = Entry]':
../src/TypedListDemo.cpp:115:   instantiated from here
D:/Bada/1.0.0/include/FBaseColLinkedListT.h:606: error: no matching function for call to 'Entry::Entry()'
D:/Workspace/Bada/Experimental/TypedListDemo/inc/Entry.h:26: note: candidates are: Entry::Entry(const Osp::Base::String&, const Osp::Base::String&)
D:/Workspace/Bada/Experimental/TypedListDemo/inc/Entry.h:15: note:                 Entry::Entry(const Entry&)
D:/Bada/1.0.0/include/FBaseColLinkedListT.h: In member function 'result Osp::Base::Collection::LinkedListT<Type>::ContainsAll(const Osp::Base::Collection::ICollectionT<Type>&, bool&) const [with Type = Entry]':

So now, the problem is that my Entry class did not have a default constructor with no arguments. Looking back at the documentation for LinkedListT and ArrayListT I was not able to find any mention of this. Nevertheless, I added the required constructor and everything compiled fine. In the end, the Entry class was looking like:

class Entry
{
private:
    String _pFirstName;
    String _pLastName;

public:
    // This default construct is required so that it can be used with
    // LinkedListT and ArrayListT
    Entry()
    {
    }
    ;
    Entry(const String &firstName, const String &lastName);
    const String& GetFirstName();
    const String& GetLastName();

    // These are provided for LinkedListT and ArrayListT
    bool operator==(const Entry& object) const;
    bool operator!=(const Entry& object) const;
    Entry& operator=(const Entry& rhs);

    virtual ~Entry();
};
Advertisements

One Response to [Bada] Creating a LinkedListT or ArrayListT to Hold Your Own Type of Objects

  1. Irinel says:

    Very useful, thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: