Jump to content

C++ ArrayList Help

- - - - -

  • Please log in to reply
1 reply to this topic

#1
reaper

reaper

    Newbie

  • Members
  • PipPip
  • 11 posts
So I am working on writing an ArrayList class in C++ after taking a data structures course this year in Java. I prefer native code over Java and am really doing this just for fun. But g++ won't compile because of a problem with my get method.

Header
template <class T> class arraylist {

        private:

           T * data; // Pointer to the dynamically sized array of data

           int size; // Current size of our arraylist

        public:

           arraylist(); // Construct the arraylist

           void add(T); // Add a new item to the list, should increment size


           void remove(int); // Remove item at index, should decrement size

           int find(T); // Given an item, find its index, return -1 if not found


           int getSize(); // Return the current size of the arraylist

           T * get(int); // Return a pointer to item at a int index

};

But the implementation seems to be giving me troubles.

template <class T> T* arraylist<T>::get(int idx) {

        // Return a pointer to the item at idx index

        if(idx > -1 && idx < size) return data[idx];

        else return NULL;

}

And here is the file I'm using to test:

#include <iostream>

#include <string>

#include "arraylist.h"

using namespace std;


int main() {

        string str1 = "foo";

        string str2 = "bar";

        string str3 = "baz";

        string str4 = "qux";


        arraylist<string> ar (); // Create an arraylist and construct it.

        if(ar.get(5) != NULL) cout << "Something was returned!" << endl;

        return 0;

}

Gnu C++ Compiler throws this error:

test-arraylist.cpp:15:16: error: request for member 'get' in 'ar', which is of n
on-class type 'arraylist<std::basic_string<char> >()'

I've been confused on this one for awhile now, but I guess its probably because I'm coming over to C++.

Could anyone help?

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
I think i will need to see the rest of your code to fix the problem.

Following is what i did so far and got a successful compilation with your existing code.

Since in my case constructor was not defined so i simply changed it to


arraylist() {} // Construct the arraylist


Mainly removed the parentheses for constructor calling although it shouldn't make a difference but we can figure that out latter. Templates have weird errors since compiler often misinterprets our intensions. I just changed type to int since it was easier for me think in terms of int and look for problems.


arraylist<int> ar; // Create an arraylist and construct it.


Get method returns a T* so i had to add & operator below


return &data[idx];


At least there is no error now. See if you get this far and post the rest of your code.

Edited by fayyazlodhi, 19 May 2011 - 11:51 AM.
adding one missed code tag





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users