1

Here is the arrayList class:

template<class T>
class arrayList: public linearList<T> 
{

public:
    // constructor, copy constructor and destructor
    arrayList(int initialCapacity = 10);
    arrayList(const arrayList<T>&);
    ~arrayList() {
        delete[] element;
    }

void insert(int theIndex, const T& theElement);
protected:
    T* position;
}; // end of iterator class

protected:
    // additional members of arrayList
    void checkIndex(int theIndex) const;
    // throw illegalIndex if theIndex invalid
    T* element; // 1D array to hold list elements
    int arrayLength; // capacity of the 1D array
    int listSize; // number of elements in list
};

dict2 read the file and store the words in the arrayList. I userrayList<char[10]>, but how can input these into the arrayList from file? The errors are indicated in the above main() function.

And in the main:

In the main function, it has the following.

arrayList<char[10]> *dict1 = new arrayList<char[10]> (1000);

int k = 0;
while (getline(fin, str)) {
    dict1->insert(k, str.c_str()); // error here
    k++;
}

reverseArray(dict2); // error here

Edit

I should use arrayList<string> in this case.

2
  • Your container lacks a copy assignment operator. Why are you allocating the containers themselves on the heap? The purpose of a container like this is that they can manage heap-allocated objects so that you don't have to, and dynamically allocating the containers themselves defeats that purpose. Is there a reason you are not using one of the C++ Standard Library containers, like std::vector? Why can you not use an arrayList<std::string>? Commented Feb 1, 2011 at 21:12
  • @ James: This is one of my class problems. It requires to use arrayList not vector. But I will change the dynamically allocated arrayList in the main() function. Thanks! Commented Feb 1, 2011 at 21:16

2 Answers 2

2

Your insert function is declared as

void insert(int theIndex, const T& theElement); 

T is char[10], so you would need to pass it a char[10], not a const char*.

Arrays are neither assignable nor copy constructible, so if you want your container to be able to handle them, you'll need to write code specifically for when the value type is an array: they can't in general be handled in the same way that scalar objects are handled.

Sign up to request clarification or add additional context in comments.

4 Comments

If I do not modify the insert method in the arrayList. How can I modify the code in the main function? Thank you.
@Sean: You would need to have a char[10] in the main function that you could pass to insert. That isn't an ideal solution. Containers aren't generally designed to store arrays.
@Sean: Try using ArrayList<std::string> in main? arrayList<char[10]> just plain wouldn't work because arrays are not assignable (e.g your erase method wouldn't compile if you called it etc).
@ UncleBens: I should use arrayList<string>.
1

You should try to use std::string as the contained type. While that will fragment the memory a bit, it will make the code simpler, and less error prone. As it is, it will not be able to handle words that take more than 10 characters (or 9 plus the nul terminator if you want to make the words compatible with C strings), and you have to work a way of manually copying contents from the std::string that you read with getline to the array, and that will make the code a little awkward:

while ( getline( fin, str ) ) {
   char buffer[10];
   strncpy( buffer, str.c_str(), 10 ); // manually copy
   dict1->insert( k, buffer );
   dict2->insert( k, buffer );
   ++k;
}

If the container held strings instead of the fixed size character arrays, the code would be slightly simpler:

while ( getline( fin, str ) ) {
   dict1->insert( k, str );
   dict2->insert( k, str );
   ++k;
}

and much more flexible as it will be able to process this answer regardless of the length of the longest words (compatible:10, regardless:10).

2 Comments

at first, I use arrayList<string>, but there is compilation error. The erase methods uses stl::copy, so element +1 can not point to the second string in the arrayList, right? Thanks
I don't see any problem there. The erase, as implemented above should work, unless I have missed something while reading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.