我正在尝试创建一个采用模板类型并将其添加到列表/数组末尾的函数,但我遇到了一个我似乎找不到解决方法的错误。我是模板新手,所以我不确定这是否是我使用模板或其他东西的问题。
这是我拥有的代码的相对部分:
// MyArray.h
// insure that this header file is not included more than once
#pragma once
#ifndef MYARRAY_H_
#define MYARRAY_H_
template <class elemType>
class MyArray
{
private:
int _size; // number of elements the current instance is holding
int _capacity; // number of elements the current instance can hold
int _top; // Location of the top element (-1 means empty)
elemType * list; // ptr to the first element in the array
public:
// Ctors
MyArray(); // default
MyArray(int capacity); // initialize to capacity
MyArray( MyArray & original); // copy constructor
// Dtor
~MyArray();
// METHODS
// Add
// Takes an argument of the templated type and
// adds it to the end of the list/array
void Add(const elemType & elem);
};
// =============================================================================
/* ... */
// METHODS
// Add
// Takes an argument of the templated type and
// adds it to the end of the list/array
template <class T>
void MyArray<T>::Add(const elemType & elem) // error C4430 and C2143
{
list[ _size + 1 ] = elem; // Place item on the bottom of the stack
} // error C2244
#endif
我收到这些错误:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\myarray.h 80 1 Testing_Grounds
Error 2 error C2143: syntax error : missing ',' before '&' c:\...\myarray.h 80 1 Testing_Grounds
Error 3 error C2244: 'MyArray<elemType>::Add' : unable to match function definition to an existing declaration c:\...\myarray.h 83 1 Testing_Grounds
对此的任何帮助将不胜感激!