我试图实现两个2×2矩阵的strassens算法,以便制作一个递归矩阵乘法算法,但是实现不编译给我这样的错误:在范围中定义的函数,但编译器抱怨它超出范围
“STRASSEN在此范围内未声明” 和 “不合格-ID”
下面的代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int[][] strassen(int A[][2], int B[][2])
{
int s1 = B[0][1] - B[1][1];
int s2 = A[0][0] + A[0][1];
int s3 = A[1][0] + A[1][1];
int s4 = B[1][0] - B[0][0];
int s5 = A[0][0] + A[1][1];
int s6 = B[0][0] + B[1][1];
int s7 = A[0][1] - A[1][1];
int s8 = B[1][0] + B[1][1];
int s9 = A[0][0] - A[1][0];
int s10 = B[0][0] + B[0][1];
int p1 = A[0][0] * s1;
int p2 = s2 * B[1][1];
int p3 = s3 * B[0][0];
int p4 = A[1][1] * s4;
int p5 = s5 * s6;
int p6 = s7 * s8;
int p7 = s9 * s10;
int C[2][2];
C[0][0] = p5 + p4 - p2 + p6;
C[0][1] = p1 + p2;
C[1][0] = p3 + p4;
C[1][1] = p5 + p1 - p3 - p7;
return C[][];
}
int main()
{
int A[2][2] = {{1,3},{7,5}};
int B[2][2] = {{6,8},{4,2}};
int C[][2] = strassen(A,B);
cout<<C[0][0]<<endl<<C[0][1]<<endl<<C[1][0]<<endl<<C[1][1]<<endl;
return 0;
}
你能告诉我为什么我得到编译时错误。 我还需要知道如何为一个二维数组创建空间,因为当我的当前实现C
将在函数退出返回垃圾值时立即超出范围。
你应该*总是*发布你的编译器/链接器错误*逐字*。 –
“return C [] [];” - 你打算通过这个获得什么? –
忘记'malloc'的东西!不要在C++中使用它! – datell