函数在作用域中定义,但编译器抱怨它超出了作用域

Function defined in scope but the compiler complains that it is out of scope

本文关键字:作用域 定义 函数 编译器      更新时间:2023-10-16

我试图为两个 2x2 矩阵实现 strassens 算法,以便制作递归矩阵乘法算法,但实现没有编译给我这样的错误:

"斯特拉森没有在这个范围内宣布" 和 "非限定标识">

代码如下:

#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;
}

你能告诉我为什么我会收到编译时错误吗? 我还需要知道如何为 2D 数组malloc空间,因为一旦函数退出返回垃圾值,我当前的C实现就会超出范围。

正如许多评论中提到的,您的解决方案是典型的C 风格,可能会产生许多问题(尤其是当您是初学者时)。C++为C可能变得复杂的许多情况提供了功能强大,节省内存且易于使用的解决方法。

不要误会我的意思:C 是一种很棒的语言,但当你决定使用 C++ 时,使用它!

对于您的情况std::array是完美的,因为您使用明确定义大小的数组。它的工作原理是这样的:您可以使用std::array<type,size>定义其内容的大小和类型。

以下代码使用std::array实现您的尝试:

#include <iostream>
// #include <cstdlib> // use C libraries only when really needed
#include <array> 
using namespace std;
array<array<int,2>,2> strassen(array<array<int,2>,2> A, array<array<int,2>,2> B){
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;
array<array<int,2>,2> C;
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(){
array<array<int,2>,2> A  {{{{1,3}},{{7,5}}}};
array<array<int,2>,2> B  {{{{6,8}},{{4,2}}}};
array<array<int,2>,2> C = strassen(A,B);
cout<<C[0][0]<<endl<<C[0][1]<<endl<<C[1][0]<<endl<<C[1][1]<<endl;
}

就像你对 C 样式数组所做的那样,二维 arrray 被实现为数组数组,因此std::array<std::array<T,size>,size>>

对于AB初始化中看起来奇怪的大括号数量,请参阅为什么不能简单地初始化(带大括号)2D std::array?[重复]。

请注意,我在main()中初始化数组的方式需要-std=c++11编译器标志。使用类似gcc -std=c++11 -o strassen strassen.c的东西进行编译

代码无法编译有几个原因: 您得到的函数错误超出了范围,因为函数 strassen 没有编译,并且它没有编译,因为您返回了在函数内声明的数组。

一个好的经验法则是永远不要返回数组,也不要将它们作为参数传递,而是使用引用,这样可以节省内存和时间。

这是一个不使用动态内存的解决方案(尽管我认为这样做会更容易)

#include <iostream>
using namespace std;
void strassen(int (&A)[2][2], int (&B)[2][2], int (&C)[2][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;
C[0][0] = p5 + p4 - p2 + p6;
C[0][1] = p1 + p2;
C[1][0] = p3 + p4;
C[1][1] = p5 + p1 - p3 - p7;
}
int main()
{
int A[2][2] = {{1,3},{7,5}};
int B[2][2] = {{6,8},{4,2}};
int C[2][2];
strassen(A,B,C);
cout<<C[0][0]<<endl<<C[0][1]<<endl<<C[1][0]<<endl<<C[1][1]<<endl;
return 0;
}

请注意,您将 C 作为对函数的引用传递,因此您在函数内部对它所做的更改也会影响函数外部的它