Ethan
Ethan

Reputation: 1266

How to use a template class C++

I'm having a problem using a class I wrote in C++. I've just recently set out on learning C++ and I quickly searched for the proper way but after putting it in the "proper" way I still got an error....So I apologize in advance if this is just some stupid misunderstanding (although I doubt it...) the relevant code is below

ArrayList.h

#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_

#include <string>

template<class T>
class ArrayList {

    private:
         //Data fields-------------------//
        T *array;
        int size;
        int capacity;
        bool sorted;

        //Methods-----------------------//
        void reallocate();
        void reallocate(int newSize);

        T* mergeSort(T* array, int arraySize);

    public:
        //Constructors------------------//
        ArrayList();
        ArrayList(int theSize);

        //Methods-----------------------//
        //Operations
        bool add(T element);
        bool add(T element, int index);
        bool add(ArrayList<T> list);
        bool add(ArrayList<T> list, int index);
        std:string toString();
};

#endif /* ARRAYLIST_H_ */

ArrayList.cpp

#include "ArrayList.h"

//~Constructors-----------------------------------------------
/**
 * Default constructor,
 * creates a 20 element ArrayList, of type T.
 */
template<class T>
ArrayList<T>::ArrayList() {

    array = new T[20];
    capacity = 20;
    size = 0;
}

//~Methods---------------------------------------------------
/**
 * Adds the passed in element to the end of the ArrayList.
 *
 * Runs in O(n) in worst case, where reallocate is called.
 *
 * @param element the element to add to the array.
 */
template<class T>
bool ArrayList<T>::add(T element) {

    bool value = false;

    if (element != NULL) {
        if ((size - 1) == capacity) {

            value = reallocate();
        }

        if (value) {
            array[size] = element;
            size++;
        }
    }

    return value;
}

ArrayListTest.cpp

#include "ArrayList.h"
#include <iostream>

int main(void) {

    using namespace std;

    ArrayList<int> myList();

    myList.add(184387);
    myList.add(14);
    myList.add(147);
    myList.add(1);
    myList.add(-37);
    myList.add(584);
    myList.add(-2147);
    myList.add(0);
    myList.add(-75);
    myList.add(147);
    myList.add(-37);
    myList.add(0);
    myList.add(25);
    myList.add(187);
    myList.add(92);
    myList.add(-17);

    cout << myList.toString();
}

The errors occur in the TestArrayList.cpp file. I have an error on all of the add calls and on the cout call.

The error on the add calls is:

request for member 'add' in 'myList', which is of non-class type 'ArrayList<int>()'

The error on the cout call is:

Method 'toString' could not be resolved

Anyone know what I'm doing wrong here?

Upvotes: 1

Views: 4927

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258648

ArrayList<int> myList();

should be

ArrayList<int> myList;

You're running into most vexing parse (google it). Basically, ArrayList<int> myList(); doesn't declare a variable, but declares a function called myList that takes no arguments and returns an ArrayList<int>.

Some side notes:

  • you're allocating memory in the constructor, but never freeing it. You should have a destructor.
  • that means you should obey the rule of three (google it)
  • my psyching abilities (yes, I have them, keep it on the down low) tell me that your next problem is going to be that it's not linking. Move the implementations to the header. Templates must have the implementation visible to all translation units that specialize them.

Upvotes: 7

Related Questions