如何使用模板类C++

Eth*_*han 1 c++ templates class

我在使用C++编写的类时遇到了问题.我刚刚开始学习C++并且我很快就找到了正确的方法,但是在以"正确"的方式进行之后我仍然遇到了错误....所以我提前道歉,如果这只是一些愚蠢的误解(虽然我对此表示怀疑......)相关代码如下

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_ */
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

错误发生在TestArrayList.cpp文件中.我在所有添加调用和cout调用上都有错误.

添加调用的错误是:

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

cout调用的错误是:

Method 'toString' could not be resolved
Run Code Online (Sandbox Code Playgroud)

谁知道我在做错了什么?

Luc*_*ore 7

ArrayList<int> myList();
Run Code Online (Sandbox Code Playgroud)

应该

ArrayList<int> myList;
Run Code Online (Sandbox Code Playgroud)

你正在遇到最烦恼的解析(google it).基本上,ArrayList<int> myList();不声明变量,但声明一个myList不带参数的函数并返回一个ArrayList<int>.

一些旁注:

  • 你在构造函数中分配内存,但永远不会释放它.你应该有一个析构函数.
  • 这意味着你应该遵守三条规则(google it)
  • 我的精神能力(是的,我有它们,保持低位)告诉我你的下一个问题是它不会连接.将实现移动到标头.模板必须对所有专门化它们的翻译单元可见.

  • @Ethan因为它是一个模板,它需要在头文件中,因为编译器必须能够直接访问代码,无论它在何处使用. (2认同)