在c++中有错误的;未实现的构造函数
Having an error in c++; Unimplemented constructors?
我是编程新手,正在尝试实现一个简单版本的ArrayList。我遇到了一个错误,当我试图找到解决方案时,人们说这是因为声明了构造函数,但没有实现。我实现了我在头中声明的所有构造函数,所以我不确定出了什么问题。感谢您的建议!
错误1错误LNK2019:未解析的外部符号"public:__thiscallArrayList::ArrayList(void)"(??0$ArrayList@H@@QAE@XZ)在函数_main 中引用
错误2错误LNK2019:未解析的外部符号"public:void__thiscall ArrayList::add(int)"(?add@$ArrayList@H@@QAEXH@Z)在函数_main 中引用
错误3错误LNK1120:2个未解析的外部
ArrayList.h
#pragma once
#ifndef ArrayList_h
#define ArrayList_h
#include <stdexcept>
using namespace std;
template <class T>
class ArrayList
{
public:
ArrayList();
~ArrayList();
void add(T item);
void expandArray();
T get(int index);
private:
int size;
int length;
T* list;
};
#endif
//ArrayList.cpp
#include "ArrayList.h"
template <class T>
ArrayList<T>::ArrayList(){
size=1;
length=0;
list = new T(size);
for(int x=0; x<size;x++){
list[x]=NULL;
}
}
template <class T>
ArrayList<T>::~ArrayList(){
delete[] list;
}
template <class T>
void ArrayList<T>::add(T item){
if(length>=size){
expandArray();
}
list[length]=item;
length++;
}
template <class T>
void ArrayList<T>::expandArray(){
size*=2;
T* temp = new T(size);
for(int x=0;x<size;x++){
temp[x]=NULL;
}
for(int x=0;x<length;x++){
temp[x]=list[x];
}
delete[] list;
list=temp;
}
template <class T>
T ArrayList<T>::get(int index){
if(index>length||index<0){
throw out_of_range("Index out of bounds!");
}
return list[index];
}
主要.cpp
#include "ArrayList.h"
int main(){
ArrayList<int>* list = new ArrayList<int>();
for(int x=0; x<=30;x++){
list->add(x);
}
return 0;
}
模板定义需要在头文件中。将ArrayList<T>::ArrayList
构造函数定义移动到ArrayList.h中。
相关文章:
- 通过构造函数动态确定类实现
- 运算符+ 的规范实现涉及额外的移动构造函数
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何为具有私有成员的派生类实现移动构造函数
- 如何正确实现具有原始指针的类的复制构造函数?
- 复制构造函数和赋值运算符的值类实现
- 如何实现共享数据的复制构造函数
- 嵌套的模板结构构造函数实现
- 为什么<T> LLVM 中的预期为 Expect&&... 实现两个构造函数<T>?
- 在C++中正确实现复制构造函数
- 如何实现构造函数,使其仅接受使用 typeid 的输入迭代器?
- 如何将深拷贝构造函数实现到链表中?
- 如何在派生类上强制实现特定数据类型的构造函数?
- 如何实现内联显式类的构造函数?
- C++头/实现文件中的默认和重载构造函数?
- C++:默认构造函数实现
- std::sort 一个实现了移动构造函数的类
- 实现标记联合的移动构造函数
- 我如何实现从其基类获取变量的构造函数
- 实现构造函数时出现LNK2019错误