who can help me.

C、C++和Java语言
回复
jinwp2007
帖子: 8
注册时间: 2007-11-01 9:04
送出感谢: 0
接收感谢: 0

who can help me.

#1

帖子 jinwp2007 » 2007-11-04 14:52

I design a complex class to solve a dif, but have many error .who can help me?
#include <math.h>
#include <iostream>
#define N 16
#define PI 3.1415926


class complex
{
public:
double real;
double imag;
public:
complex(double r=0,double i=0)
{
real=r;
imag=i;
}

complex operator+(complex &a)
{
complex result;
result.real=a.real+real;
result.imag=a.imag+imag;
return result;
}

complex operator-(complex &a)
{
complex result;
result.real=real-a.real;
result.imag=imag-a.imag;
return result;
}

complex operator*(complex &a)
{
complex result;
result.real=a.real*real-a.imag*imag;
result.imag=a.real*imag+a.imag*real;
return result;
}

complex operator/(complex &a)
{
complex result;
result.real=(real*a.real+imag*a.imag)/(real*real+imag*imag);
result.imag=(real*a.imag-a.real*imag)/(real*real+imag*imag);
return result;
}

void display()
{
if(real!=0)
std::cout<<real;
if(imag>0)
std::cout<<"+"<<imag<<"i";
if(imag<0)
std::cout<<imag<<"i";
std::cout<<std::endl;
}
};

complex w(int a,int n)
{
complex result;
result.real=cos(2*PI/n*a);
result.imag=-sin(2*PI/n*a);
if(fabs(result.real)<0.00001)
result.real=0;
if(fabs(result.imag)<0.00001)
result.imag=0;
return result;
}

void swap(complex *t1,complex *t2,int n)
{
complex temp;
for(int i=0;i<n;i++)
{
temp=t1[i];
t1[i]=t2[i];
t2[i]=temp;
}
}

void dft(complex *p1,complex *p2,int n)
{
for(int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
p2[i]=p2[i]+p1[j]*w(j*i,n);
}
}

void dif(complex *p1,complex *p2,int n)
{
int i,j,k p,s;

for(p=2;p<n;p*=2)
{
for(j=0,k=1;j<p;k*=2,j+=k)
{
s=n/p;
for(i=0;i<s;i++)
{
p2[i+j]=p1[i+j]+p1[i+j+p];
p2[i+j+p]=p1[i+j]-p1[i+j+p]*w(i,s*2);
}
}
swap(p1,p2,n);
}
dft(p2.p1,n);
for(i=0;i<n;i++)
p1[i].display();
}

int main()
{
complex temp1[N],temp2[N];
int i;
int x[N]={1,1,2,2,3,3,4,4,1,1,2,2,0,0,0,0};

for(i=0;i<N;i++)
{
temp1[i].real=x[i];
temp1[i].imag=0;
}
complex *p1,*p2;
p1=temp1;
p2=temp2;

dif(p1,p2,N);

return 0;
}

ERROR:
main.cc: In function ‘void dft(complex*, complex*, int)’:
/home/linux/Projects/foobar-cpp/src/main.cc:114: error: no match for ‘operator*’ in ‘*(((complex*)(((unsigned int)j) * 16u)) + p1) * w((j * i), n)’
/home/linux/Projects/foobar-cpp/src/main.cc:58: note: candidates are: complex complex::operator*(complex&)
main.cc: In function ‘void dif(complex*, complex*, int)’:
/home/linux/Projects/foobar-cpp/src/main.cc:120: error: expected initializer before ‘p’
/home/linux/Projects/foobar-cpp/src/main.cc:122: error: ‘p’ was not declared in this scope
/home/linux/Projects/foobar-cpp/src/main.cc:124: error: ‘k’ was not declared in this scope
/home/linux/Projects/foobar-cpp/src/main.cc:126: error: ‘s’ was not declared in this scope
/home/linux/Projects/foobar-cpp/src/main.cc:135: error: request for member ‘p1’ in ‘p2’, which is of non-class type ‘complex*’
make[2]: *** [main.o] Error 1
make[2]: Leaving directory `/home/linux/Projects/foobar-cpp/src'
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
make[1]: Leaving directory `/home/linux/Projects/foobar-cpp'
Completed... unsuccessful
jinwp2007
帖子: 8
注册时间: 2007-11-01 9:04
送出感谢: 0
接收感谢: 0

#2

帖子 jinwp2007 » 2007-11-04 15:12

the question is work out , i want to ask the another question.
how to setup the chinese input?
atnet
帖子: 125
注册时间: 2006-12-02 8:58
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 atnet » 2007-11-04 16:50

may be scope error ?
yes ,you know cout must write as std:cout
but,there is so much place that you must be careful !

if this program is work good at Windows or mac
i think if you change your c++ complie ,you can do it success to
last week, I do a project , if i use gcc *.cpp,there is so much error
but ,if i use g++ *.c ,It work!

setup chinese input ?
I suggest you can open http://wiki.ubuntu.org.cn 点击链接(你的版本)快速设置指南
http://www.ieager.com/
我的blog。linux学习
回复

回到 “C/C++/Java”