help me rectify the code---why cant i initialize the double variable sum1=0

  • Thread starter Shashidhar Patil
  • Start date
S

Shashidhar Patil

here is the code

#include<iostream.h>
#include<math.h>
#include<conio.h>
class legendre
{
private:
int c,b,z,zz,xx,yy,fact;
double a,nt,x,y,aa,final,bb,cc;
double sum1=0;
public:
int factorial(int n)
{
if (n==0)
return 1;
else
{
fact=n*factorial(n-1);
return fact;
}
}
// legendre();
void calculate(int l,int m,double theta)
{
a=(2*l+1)/2;
b=1- m%1;
c=1+ m%1;
zz=factorial(l);
xx=factorial(c);
yy=factorial(z);
bb=factorial(b)/zz;
cc=factorial(c)/zz;
nt=sqrt(a*bb*cc);
for(int q=0; q<=b/2; q++)
{
z=2*l-2*q;
x=z/(factorial(l-q)*factorial(q));
y=yy/(xx*factorial(z-c));
sum1+=((pow(-1,l+q))*x*y*(pow(cos(theta),b-2*q)));
}

aa=nt*(pow(sin(theta),m)/pow(2,l));
final=aa*sum1;
cout<<"the value of the expression : "<<final<<endl;
}
};

void main()
{
int l,m;
double theta;
cout<<"enter the values for l, m and theta: "<<endl;
cin>>l>>m>>theta;
legendre expression;
expression.calculate(l,m,theta);
getch();
}
 
H

Howard

Shashidhar Patil said:
here is the code

#include<iostream.h>
#include<math.h>
#include<conio.h>
class legendre
{
private:
int c,b,z,zz,xx,yy,fact;
double a,nt,x,y,aa,final,bb,cc;
double sum1=0;
public:


When you add an initializer statement to a declaration, it becomes a
definition, not a declaration, and definitions are not allowed within a
class declaration. (See section 4.9 of the standard: Any declaration that
specifies a value is a definition.)

-Howard
 
L

llewelly

here is the code

#include<iostream.h>
#include<math.h>
#include<conio.h>
class legendre
{
private:
int c,b,z,zz,xx,yy,fact;
double a,nt,x,y,aa,final,bb,cc;
double sum1=0;

An object is not initialized by a class definition. An object is
initialized by its constructor. So this is wrong.

All of these variables should be initialized by entries in the
mem-initializer list.

See http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
public:
int factorial(int n)
{
if (n==0)
return 1;
else
{
fact=n*factorial(n-1);
return fact;
}
}
// legendre();

You need:

legendre()
:c(0),b(0),z(0),zz(0),xx(0),yy(0),fact(0)
,a(0),nt(0),x(0),y(0),aa(0),final(0),bb(0),cc(0)
,sum1(0) // <- important.
{}

I initialized all of your member variables in the above
mem-initializer list. Any member variables not listed in the
mem-initializer list will be default-initialized (before the
constructor begins). You must decide wether you want your data
members initialized with 0, or default-initialized (with undefined
values).

legendre():sum1(0) {}

would also be ok, but all of your other data members would be
default-initialized, and have unknown values.

Notice the variables in the mem-initializer list are in the same order
as the variables occur in the class definition above. Data members
are always initialized in the order they are declared in, *not*
the order they appear in the mem-initializer list. Some compilers
will warn if the mem-initializer list is in a different order.
void calculate(int l,int m,double theta)
{
a=(2*l+1)/2;
b=1- m%1;
c=1+ m%1;
zz=factorial(l);
xx=factorial(c);
yy=factorial(z);
bb=factorial(b)/zz;
cc=factorial(c)/zz;
nt=sqrt(a*bb*cc);
for(int q=0; q<=b/2; q++)
{
z=2*l-2*q;
x=z/(factorial(l-q)*factorial(q));
y=yy/(xx*factorial(z-c));
sum1+=((pow(-1,l+q))*x*y*(pow(cos(theta),b-2*q)));
}

aa=nt*(pow(sin(theta),m)/pow(2,l));
final=aa*sum1;
cout<<"the value of the expression : "<<final<<endl;
}
};
[snip]
 
K

Kevin Goodsell

Shashidhar said:
here is the code

#include<iostream.h>

This is not a standard header. It comes from pre-standard C++, and has
been outdated for about 6 years now. Standard C++ uses said:
#include<math.h>

Technically still allowed said:
#include<conio.h>

This header has never been part of C++.
class legendre
{
private:
int c,b,z,zz,xx,yy,fact;
double a,nt,x,y,aa,final,bb,cc;
double sum1=0;

As others have explained, you can't do this.

void main()

In the history of C++ (and C), there has never been a time in which
'void' was an acceptable return type for main. main must return int.
{
int l,m;
double theta;
cout<<"enter the values for l, m and theta: "<<endl;
cin>>l>>m>>theta;
legendre expression;
expression.calculate(l,m,theta);
getch();

Not a standard C++ function.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top