1

我写了一个头文件,声明了一个使用三个多维数组作为参数的函数,并在另一个 .cpp 文件中定义了它。看起来不错,但在链接时找不到该函数。这是头文件:

#ifndef StrassenAlgorithms_Algorithm_h
#define StrassenAlgorithms_Algorithm_h
template<int size>
void strassen_matrix_multiplication(int A[size][size], int B[size][size], int C[size][size]);

#endif

然后是对应的.cpp文件:

#include "Algorithm.h"

template<int size>
void strassen_matrix_multiplication(int (*A)[size], int (*B)[size], int (*C)[size]){
if (size == 1) {
    return;
}

const int n = size / 2;
int A11[n][n];
int A12[n][n];
int A21[n][n];
int A22[n] [n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        A11[i][j] = A[i][j];
        A12[i][j] = A[i + n][j];
        A21[i][j] = A[i][j + n];
        A22[i][j] = A[i + n][j + n];
    }
}

int B11[n][n], B12[n][n], B21[n][n], B22[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        B11[i][j] = A[i][j];
        B12[i][j] = A[i + n][j];
        B21[i][j] = A[i][j + n];
        B22[i][j] = A[i + n][j + n];
    }
}

/** S1 = B12 - B22 */
int S1[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S1[i][j] = B[i + n][j] - B[i + n][j + n];
    }
}

/** S2 = A11 + A12 */
int S2[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S2[i][j] = A[i][j] + A[i + n][j];
    }
}

/** S3 = A21 + A22 */
int S3[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S3[i][j] = A[i][j + n] + A[i + n][j + n];
    }
}

/** S4 = B21 - B11 */
int S4[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S4[i][j] = B[i][j + n] - B[i][j];
    }
}

/** S5 = A11 + A22 */
int S5[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S5[i][j] = A[i][j] + A[i + n][j + n];
    }
}

/** S6 = B11 + B22 */
int S6[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S6[i][j] = B[i][j] + B[i + n][j + n];
    }
}

/** S7 = A12 - A22 */
int S7[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S7[i][j] = A[i + n][j] - A[i + n][j + n];
    }
}

/** S8 = B21 + B22 */
int S8[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S8[i][j] = B[n][j + n] + B[i + n][j + n];
    }
}

/** S9 = A11 - A21 */
int S9[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S9[i][j] = A[i][j] - A[i][j + n];
    }
}

/** S10 = B11 + B12 */
int S10[n][n];
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        S10[i][j] = B[i][j] + B[i + n][j];
    }
}

int P1[n][n];
strassen_matrix_multiplication(A11, S1, P1);

int P2[n][n];
strassen_matrix_multiplication(S2, B22, P2);

int P3[n][n];
strassen_matrix_multiplication(S3, B11, P3);

int P4[n][n];
strassen_matrix_multiplication(A22, S4, P4);

int P5[n][n];
strassen_matrix_multiplication(S5, S6, P5);

int P6[n][n];
strassen_matrix_multiplication(S7, S8, P6);

int P7[n][n];
strassen_matrix_multiplication(S9, S10, P7);

/** C11 = P5 + P4 - P2 + P6 **/
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        C[i][j] = P5[i][j] + P4[i][j] - P2[i][j] + P6[i][j];
    }
}

/** C12 = P1 + P2 **/
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        C[i + n][j] = P1[i][j] + P2[i][j];
    }
}

/** C21 = P3 + P4 **/
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        C[i][j + n] = P3[i][j] + P4[i][j];
    }
}

/** C22 = P5 + P1 - P3 - P7 **/
for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
        C[i + n][j + n] = P5[i][j] + P1[i][j] - P3[i][j] - P7[i][j];
    }
}

return;    
}

最后是主文件:

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

int main(int argc, const char * argv[])
{
const int size = 2;
int A[size][size] = {{1, 2}, {3, 4}};
int B[size][size] = {{1, 2}, {3, 4}};
int C[size][size] = {0, 0, 0, 0};
strassen_matrix_multiplication<2>(A, B, C);
for(int i=0; i<size; i++){
    for (int j=0; j<size; j++) {
        cout << C[i][j] << "\t";
    }
    cout << endl;
  }
}

它没有编译错误,但在链接时失败说

Undefined symbols for architecture x86_64:
"void strassen_matrix_multiplication<2>(int (*) [2], int (*) [2], int (*) [2])", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我该如何解决这个问题?谢谢你的帮助。在线等待。

4

1 回答 1

1

您的问题是模板方法和类不能在目标文件中实现,而必须在标头中实现,以便编译器可以进行模板扩展。除此之外,即使您可以,这些方法签名似乎也与我不完全一致。

于 2012-12-22T15:28:13.943 回答