作者:潘PanPanPq | 来源:互联网 | 2023-09-23 16:41
错误如下:e:\programfiles\microsoftvisualstudio12.0\vc\include\stdio.h(211):参见“fopen”的声明1>
错误如下:
e:\program files\microsoft visual studio 12.0\vc\include\stdio.h(211) : 参见“fopen”的声明
1>fftm1源.obj : error LNK2005: "void __cdecl fft1(double (* const)[1024],int)" (?fft1@@YAXQAY0EAA@NH@Z) 已经在 fft1.obj 中定义
1>fftm1源.obj : error LNK2005: "void __cdecl initial(void)" (?initial@@YAXXZ) 已经在 fft1.obj 中定义
1>fftm1源.obj : error LNK2005: "unsigned long __cdecl inverseq(unsigned long,unsigned long)" (?inverseq@@YAKKK@Z) 已经在 fft1.obj 中定义
1>E:\aa\research分类\8.code\C\ji2fft\Debug\ji2fft.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
fftm1源.cpp程序如下:
#define N 1024
#include"fft1.cpp"
void functf(double A[2][N]);
int main()
{
unsigned long int i;
static double A[2][N];
FILE *fp;
fp = fopen("fftm1.d","w");
system("cls");
functf(A);
fprintf(fp, "The original data,AK:\n");
printf("The original data,AK:\n");
for (i = 0; i < N; i++)
fprintf(fp, "%4lu:%15.9f%16.9%ei\n", i, A[0][i], A[1][i]);
fft1(A, 1);
fprintf(fp, "FFT,dtrect transform(AK->xj),xj:\n");
printf("FFT,dtrect transform(AK->xj),xj:\n");
for (i = 0; i < N;i++)
fprintf(fp, "%4lu:%15.9f%16.9%ei\n", i, A[0][i], A[1][i]);
fft1(A, -1);
fprintf(fp, "FFT,inverse transform(x->A),AK:\n");
printf("FFT,inverse transform(x->A),AK:\n");
for (i = 0; i < N; i++)
fprintf(fp, "%4lu:%15.9f%16.9%ei\n", i, A[0][i], A[1][i]);
fclose(fp);
printf("OK!\n");
return 0;
}
void functf(double A[2][N])
{
unsigned long int i;
double dt, tt, A0, A1;
dt = 0.1;
tt = (double)N*dt;
A0 = exp(-tt);
for (i = 0; i < N; i++)
{
tt = (double)i*dt;
A1 = exp(-tt);
A[0][i] = (A1 + A0 / A1)*dt;
A[1][i] = 0.0;
}
}
fft1.cpp程序如下:
#include
#include
#include
#ifndef N //'if not defined'的缩写,防止头文件的重复包含和编译
#define N 1024//如果N没有被宏定义过,定义N
#endif //终止if
#define eps 1.0e-9
void initial(void);//initial函数
void fft1(double A[2][N], int ifft);//fft1函数
unsigned long int inverseq(unsigned long int i1, unsigned long int imax);//inverseq函数
static int ifirst = 0, sign = -1;
static unsigned M, jup[16], kup[16];
static double pi, wr[N], wi[N];
void initial(void)
{
unsigned long int i, j, ni;
double unit, tt;
ni = N;
M = 0;
while (ni > 1)
{
ni = ni / 2;
M = M + 1;
}
ni = 1;
for (j = 0; j < M; j++)
ni = ni * 2;
if ((ni - N) != 0)
{
printf("N isn't compatible!\nStrike any key to exit!\n");
getchar();//getchar函数从标准输入里读取下一个字符,返回类型为int型,返回值为用户输入的ASCⅡ码,出错返回EOF。使用的时候要包含stdio.h头文件
exit(1);
}
kup[0] = N / 2;
jup[0] = 1;
for (j = 1; j< M; j++)
{
kup[j] = kup[j - 1] / 2;
jup[j] = jup[j - 1] * 2;
}
pi = 3.1415;
unit = 2.0*pi / ((double)N);
tt = 0.0;
for (i = 0; i < N; i++)
{
wr[i] = cos(tt);
wi[1] = sign*sin(tt);
tt = tt + unit;
}
}
unsigned long int inverseq(unsigned long int i1, unsigned long int imax)
{
unsigned long int j, ii, i0, inv;
i0 = i1;
inv = 0;
for (j = 0; j < imax; j++)
{
ii = i0 % 2;
inv = 2 * inv + ii;
i0 = (i0 - ii) / 2;
}
return(inv);
}
void fft1(double A[2][N], int ifft)
{
unsigned long int l, k, i, j, jl0, jl0k, jl1k, reseq, ij;
double wrc, wis, ar, ai, x[2][N];
if (ifirst == 0)
{
initial(),
ifirst = 1;
}
if (ifft == -1)
for (i = 0; i < M; i++)
A[1][i] = -A[1][i];
for (l = 0; l < M; l++)
{
for (j = 0; j < jup[l]; j++)
{
reseq = inverseq(j, l);
ij = kup[l] * reseq;
jl0 = 2 * kup[l] * j;
wrc = wr[ij];
wis = wi[ij];
for (k = 0; k < kup[l]; k++)
{
jl0k = jl0 + k;
jl1k = jl0 + kup[l] + k;
ar = A[0][jl1k] * wrc - A[1][jl1k] * wis;
ai = A[0][jl1k] * wis - A[1][jl1k] * wrc;
A[0][jl1k] = A[0][jl0k] - ar;
A[1][jl1k] = A[1][jl0k] - ai;
A[0][jl0k] = A[0][jl0k] + ar;
A[1][jl0k] = A[1][jl0k] + ai;
}
}
}
for (i = 0; i < N; i++)
{
j = inverseq(i, M);
x[0][i] = A[0][j];
x[1][i] = A[1][j];
}
for (i = 0; i {
A[0][i] = x[0][i];
A[1][i] = x[1][i];
}
if (ifft == -1)
for (i = 0; i < N; i++)
{
A[0][i] = A[0][i] / ((double)N);
A[1][i] = -A[1][i] / ((double)N);
}
}
曾经用 #pragma once试过仍报错。用extern也声明过,也没有用。可能是我用的方法不对吧,感觉是头文件或者是全局变量出了问题,但是实在不知道应该怎么改,求指导!谢谢
4 个解决方案
#include"fft1.cpp"
----------------->
#include"fft1.h" ???
新建一个fft1.h 头文件,把
void initial(void);//initial函数
void fft1(double A[2][N], int ifft);//fft1函数
unsigned long int inverseq(unsigned long int i1, unsigned long int imax);//inverseq函数
这三个函数放到fft1.h中进行声明,fft1.cpp中#include fft1.h后仅作那些函数的定义实现
然后在fftm1源.cpp中#include"fft1.h" 即可,不要#include"fft1.cpp"