【发布时间】:2015-01-17 19:21:14
【问题描述】:
我编写了一个 c++ 程序来使用动态编程计算矩阵乘法问题。我使用s[][] 来存储插入括号的位置。但是,我得到了关于使用二维数组作为参数的错误:
matrics.cpp:4:38: error: expected ')'
void findtrace(int i, int j, int[][7]s){
^
matrics.cpp:4:15: note: to match this '('
void findtrace(int i, int j, int[][7]s){
^
matrics.cpp:8:17: error: use of undeclared identifier 's'
printf("%d\t",s[i][j]);
^
matrics.cpp:9:18: error: use of undeclared identifier 's'
findtrace(i,s[i][j],s);
^
matrics.cpp:10:13: error: use of undeclared identifier 's'
findtrace(s[i+1][j],j,s);
^
matrics.cpp:42:2: error: no matching function for call to 'findtrace'
findtrace(1,len-1,s);
^~~~~~~~~
matrics.cpp:4:6: note: candidate function not viable: no known conversion from 'int [len][len]' to 'int (*)[7]' for 3rd argument
void findtrace(int i, int j, int[][7]s){
^
5 errors generated.
调试这个花了我两个小时;但是,我仍然有这个错误。任何人都可以帮助我吗?
#include <iostream>
using namespace std;
void findtrace(int i, int j, int[][7] s){
if(i==j)
printf("%d\t", i);
else{
printf("%d\t",s[i][j]);
findtrace(i,s[i][j],s);
findtrace(s[i+1][j],j,s);
}
}
int main(){
int p[] = {30,35,15,5,10,20,25};
int len = sizeof(p)/sizeof(p[0]);
//void findtrace(int[][len]s, int i, int j);
int m[len][len];
int s[len][len];
printf("len p = %d\n",len);
for(int i=1;i<=len;i++)
m[i][i] = 0;
int temp;
for(int k=1;k<len;++k){
for(int j=k+1;j<len;++j){
int i = j-k;
m[i][j] = m[i][i]+m[i+1][j]+p[i]*p[i-1]*p[j];
s[i][j] = i;
for(int t=i+1;t<j;++t){
if( m[i][t]+m[t+1][j]+p[i-1]*p[j]*p[t]<m[i][j] )
m[i][j] = m[i][t]+m[t+1][j]+p[i-1]*p[j]*p[t];
s[i][j] = t;
}
//printf("m[%d][%d] = %d\t",i,j,m[i][j]);
}
}
printf("\n%d\n",m[1][len-1]);
findtrace(1,len-1,s);
}
【问题讨论】:
-
您能找出导致您的第一个错误的原因吗?你知道那条消息是什么意思吗?
-
用代码发布问题时,请不要包含行号。它使复制粘贴代码变得更加困难。而是以其他方式标记问题所在的行。
-
另外,您确定您正在使用 C++ 编程吗?代码中唯一特定于 C++ 的内容是标头
<iostream>和using namespace std,其他一切都只是 C(例如使用printf和可变长度数组)。 -
@JoachimPileborg 很抱歉造成混淆,我实际上是用 C++ 编写的,但我是一名学习者,并且使用了一些 C 语法。谢谢。
-
@DrewDormann 对不起,我是一个新的 C++ 程序员,我不太确定。
标签: c++ arrays parameter-passing