问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
----------------------------------------------------array.h文件
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using namespace std;
template <class T>
class LinerList
{
private:
int length; //元素个数
T *element;
int maxSize; //数组的最大元素
public:
LinerList(int llmaxSize);
~LinerList();
LinerList<T> &Insert(int k, const T &x);
friend ostream &operator<<(ostream &out, LinerList<T> &s);
};
#endif
--------------------------------------------------------------fuc.cpp文件
#include <iostream>
#include "array.h"
using namespace std;
//实现构造函数
template <class T>
LinerList<T>::LinerList(int llmaxSize)
{
maxSize = llmaxSize;
element = new T[llmaxSize];
length = 0;
}
//实现析构函数
template <class T>
LinerList<T>::~LinerList()
{
delete[] element;
}
//在第k个位置插入元素x
template <class T>
LinerList<T>& LinerList<T>::Insert(int k, const T &x)
{
if (k < 1 || k > length + 1)
cout << "所插入的元素越界了" << endl;
else
{
if (length == maxSize)
cout << "此表已满,无法插入新的元素" << endl;
else
{
for (int i = k - 1; i < maxSize; i++)
element[i + 1] = element[i];
element[k - 1] = x;
length++;
}
}
return *this;
}
//运算符重载
template <class T>
ostream &operator<<(ostream &out, LinerList<T> &s)
{
for (int i = 0; i < s.length; i++)
out << s.element[i] << " " << endl;
return out;
}
--------------------------------------------------------------------main.cpp文件
#include <iostream>
#include "array.h"
using namespace std;
int main()
{
LinerList<int> s1(10);
s1.Insert(1, 10);
// s1.Insert(2, 20);
// s1.Insert(3, 30);
cout << s1;
return 0;
}
运行结果及报错内容
Undefined symbols for architecture arm64:
"LinerList::Insert(int, int const&)", referenced from:
_main in main-0d85b3.o
"LinerList::LinerList(int)", referenced from:
_main in main-0d85b3.o
"LinerList::~LinerList()", referenced from:
_main in main-0d85b3.o
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits >&, LinerList&)", referenced from:
_main in main-0d85b3.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)