我在使用我用 C++ 编写的类时遇到问题。我最近刚刚开始学习 C++,我很快就找到了正确的方法,但是在以“正确”的方式输入之后,我仍然遇到了错误......所以如果这只是一些愚蠢的误解,我提前道歉(虽然我对此表示怀疑......)相关代码如下
数组列表.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_ */
数组列表.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();
}
TestArrayList.cpp 文件中出现错误。我在所有 add 调用和 cout 调用上都有错误。
添加调用的错误是:
request for member 'add' in 'myList', which is of non-class type 'ArrayList<int>()'
cout 调用的错误是:
Method 'toString' could not be resolved
有人知道我在这里做错了什么吗?