相关疑难解决方法(0)

为什么模板只能在头文件中实现?

引自C++标准库:教程和手册:

目前使用模板的唯一可移植方法是使用内联函数在头文件中实现它们.

为什么是这样?

(澄清:头文件不是唯一的便携式解决方案.但它们是最方便的便携式解决方案.)

c++ templates c++-faq

1660
推荐指数
14
解决办法
46万
查看次数

使用模板时,为什么会出现"未解析的外部符号"错误?

当我使用模板为类编写C++代码并在源(CPP)文件和标题(H)文件之间拆分代码时,在链接最终可执行文件时会出现大量"未解析的外部符号"错误,尽管目标文件正确构建并包含在链接中.这里发生了什么,我该如何解决?

c++ linker templates

92
推荐指数
2
解决办法
6万
查看次数

共享库中未使用的模板函数

我在用C++编写的共享库中有一个模板函数(该函数不在库中的任何地方调用,所以不应该生成它,我错了吗?)[g ++,Linux]

我尝试在应用程序中使用此模板函数,但编译器给出链接错误.我使用objdump搜索函数但我无法在.so中看到函数有没有办法解决这个问题?

c++ templates

2
推荐指数
2
解决办法
1339
查看次数

未解析的外部符号 - 模板类

可能重复:
C++模板,链接错误

我正在尝试实现选择排序,但我不断收到错误(打印在下面).在我看来,我的所有包含和模板都正确完成.有人可以向我解释这个错误的原因以及调试此类错误的一般方法.通常情况下会出现包含或模板问题,但偶尔会出现在我不知道错误的情况下.谢谢.

错误LNK2019:函数_main中引用的未解析的外部符号"public:void __thiscall Selection :: SelectionSort(int*const,int)"(?SelectionSort @?$ Selection @ H @@ QAEXQAHH @ Z)

TEST.CPP

#include <iostream>
#include "SelectionSort.h"
using namespace std;

void main()
{
    int ar[] = {1,2,3,4,5};
    Selection<int> s;
    s.SelectionSort(ar,5);

    for(int i = 0; i < 5; i++)
    {

        cout << "\nstudent number " << i + 1<< " grade " << ar[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

SelcectionSort.h

template<class ItemType>
class Selection
{
public:
    void SelectionSort(ItemType[], int);
private:
    int MinIndex(ItemType[], int, int);
    void Swap(ItemType& , ItemType&);
};
Run Code Online (Sandbox Code Playgroud)

SelectionSort.cpp …

c++ templates

2
推荐指数
1
解决办法
6364
查看次数

标签 统计

c++ ×4

templates ×4

c++-faq ×1

linker ×1