【问题标题】:Implementing static templates in .cpp [duplicate]在.cpp中实现静态模板[重复]
【发布时间】:2013-01-25 17:39:38
【问题描述】:

可能重复:
Why can templates only be implemented in the header file?

我正在研究一个 Matrix 类,并且我有一个方法 SetMatrix,我用它来用标准 c++ 二维数组初始化数组。

template <int width, int height>
static void SetMatrix(Matrix& matrix, double array[][width]) {
    if (matrix.height_ != height || matrix.width_ != width) {
        fprintf(stderr, "Incorrect matrix size\n");
        return;
    }
    for (int y = 0; y < height; ++y)
        for (int x = 0; x < width; ++x)
            matrix.matrix_[y][x] = array[y][x];
}

并且该方法运行良好,但是当我尝试将其从头文件移动到 .cpp 实现文件时,出现链接器错误。

矩阵.cpp:

template <int width, int height>
void Matrix::SetMatrix(Matrix& matrix, double array[][width]) {
    if (matrix.height_ != height || matrix.width_ != width) {
        fprintf(stderr, "Incorrect matrix size\n");
        return;
    }
    for (int y = 0; y < height; ++y)
        for (int x = 0; x < width; ++x)
            matrix.matrix_[y][x] = array[y][x];
}

g++ 输出:

g++ -Wall -Wextra main.cpp Matrix.cpp -o matrix
/tmp/ccMkObBs.o: In function `main':
main.cpp:(.text+0x81): undefined reference to `void Matrix::SetMatrix<2, 3>(Matrix&, double (*) [2])'
collect2: error: ld returned 1 exit status

我想知道如何将它移动到 .cpp 文件中,或者我只是不走运,或者是否有更优雅的方式来做到这一点?我发现的大部分资源都是处理模板类的静态方法,而不是非模板类的静态模板方法。如果有帮助,这也是我的调用代码

int main() {
    double input[3][2] = {
        {1, 2},
        {3, 4},
        {5, 6}
    };

    Matrix matrix(2, 3);

    Matrix::SetMatrix<2, 3>(matrix, input);
    matrix.Print();
}

【问题讨论】:

标签: c++ templates static header


【解决方案1】:

main.cpp 看不到SetMatrix 成员函数模板的定义,因此在调用它时无法实例化它。编译器只能依赖于在处理其他一些翻译单元(即其他一些.cpp 文件)时已经或将要生成该函数的代码这一事实。

然而,matrix.cpp 中仅仅存在 SetMatrix 函数的定义并不意味着函数模板在此处被实例化(事实上,它没有,因为没有一行该文件中的代码调用SetMatrix);因此,不会生成该函数的目标代码。

所以最终实例化函数模板的代码不会出现在任何翻译单元中,并且会引发链接器错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多