weixin_43306709 2022-02-26 15:14 采纳率: 77.3%
浏览 28
已结题

用的vscode编译器,电脑是Mac m1芯片,linux也试着编译过,但为什么显示没有关联啊,最后的运行结果,我是通过g++ main.cpp fuc.cpp编译的

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
----------------------------------------------------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)

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • _GX_ 2022-02-26 15:27
    关注

    模板类和模板函数的定义必须放到头文件里,不能放到cpp文件里

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月13日
  • 已采纳回答 3月5日
  • 创建了问题 2月26日

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测