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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助