【发布时间】:2012-04-30 20:17:24
【问题描述】:
我写了以下递归多项式乘法,但它给了我错误,代码在这里
#include<iostream>
#include<vector>
using namespace std;
#define N 4
float *mult(float p[],float q[],int n)
{
float pl[N/2],ql[N/2],ph[N/2],qh[N/2];
float t1[N/2],t2[N/2];
float r[2*N-2],rl[N],rm[N],rh[N];
int i,N2;
if(N==1)
{
r[0]=p[0]*q[0]; return (float *)r;
}
for(i=0;i<N/2;i++)
{
pl[i]=p[i];
ql[i]=q[i];
}
for(i=N/2;i<N;i++)
{
ph[i-N/2]=p[i];
qh[i-N/2]=q[i];
}
for(i=0;i<N/2;i++) t1[i]=pl[i]*ph[i];
for(i=0;i<N/2;i++) t2[i]=ql[i]*qh[i];
rm=mult(t1,t2,N/2);
rl=mult(pl,ql,N/2);
rh=mult(ph,qh,N/2);
for(i=0;i<N-1;i++) r[i]=rl[i];
r[N-1]=0;
for(i=0;i<N-1;i++) r[N+i]=rh[i];
for(i=0;i<N-1;i++)
r[N/2+i]+=rm[i]-(rl[i]+rh[i]);
return (float *)r;
}
错误是这些
(13): warning C4172: returning address of local variable or temporary
(28): error C2440: '=' : cannot convert from 'float *' to 'float [4]' There are no conversions to array types, although there are conversions to references or pointers to arrays
(29): error C2440: '=' : cannot convert from 'float *' to 'float [4]' There are no conversions to array types, although there are conversions to references or pointers to arrays
(30): error C2440: '=' : cannot convert from 'float *' to 'float [4]' There are no conversions to array types, although there are conversions to references or pointers to arrays
(36): warning C4172: returning address of local variable or temporary
我不明白是什么原因?请帮帮我
【问题讨论】:
-
拜托,如果您通过反复试验将代码从 Java 转换为 C++,您需要停止在 SO 上发布大块代码,并在 C++ 上获得decent introductory book,以了解语言的基础知识。您的所有问题都会一口气得到解答。这大约是您在过去 2 天内采用这种格式的第 10 个问题。
-
不,我不同意你,我没有从 java 翻译成 c++,所以在有人投反对票之前,问我 dato 是不是从 java 翻译成 c++?这很粗鲁
-
对不起,这是根据您在上一个问题中所说的假设。无论如何,要点是一样的;你需要正确地学习语言; SO 不是这样做的地方。
-
我不是通过这段代码学习 c++,它是执行某些任务的示例代码,我已经厌倦了投票,如果有人认为它有助于我学习一些东西,我会开始普遍投票会告诉任何问我为什么这样做的人,这是学习的方法
-
@dato:你不是在帮助自己。您需要停止一遍又一遍地发布此类问题,并花一些时间来学习 C++ 的基础知识。然后你会发现很容易回答你自己的问题。
标签: c++ recursion polynomial-math