473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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();
}
Jul 19 '05 #1
3 3215

"Shashidhar Patil" <hi******@yahoo.com> wrote in message
news:fb************************@posting.google.com ...
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
Jul 19 '05 #2
hi******@yahoo.com (Shashidhar Patil) writes:
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-lit....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]
Jul 19 '05 #3
Shashidhar Patil wrote:
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 <iostream>.
#include<math.h>
Technically still allowed, but deprecated in favor of <cmath>.
#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.

<snip>

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3497
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
6
2769
by: polymedes | last post by:
Can some body explain to me this: Consider the following code: int main(int argc, char* argv) { double d1; d1 = 11.0; d1 -= 1.8; d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999993 !!!
3
26411
by: Joakim Hove | last post by:
Hello, I have an array of doubles, allocated with dbarr = malloc(N * sizeof(double)); I then want to set all the elements of the array to zero, this is currently done with for (i=0; i <...
1
1932
by: Joey Liang via DotNetMonster.com | last post by:
Hi all, Is it possible to store double variable to session? If possible can show me code sample on how to convert.Thanx n advance. -- Message posted via http://www.dotnetmonster.com
4
4886
by: Boni | last post by:
Dear all, how can I initialize shared variable? i.e class A private shared B as boolean end class in C++ I would write in the global scope A::B=true What is the proper way in vb
0
1002
by: robbie | last post by:
In the project upgrade from VS2002 to VS2005, all the double variables are initialize to 0.0000000. I checked the generated assembly instructions, the codes as below: double dMinVal = 0.01;...
1
1892
by: jombloboy | last post by:
I'm trying to find the biggest fibonacci number a double variable can handle, my loop looks like this: double num1 = 1; double num2 = 2; double fib = 0; { while ( fib >= 0 ) ...
3
5985
by: thatgu | last post by:
I was making a funtion that done some maths and returns a double signed variable with decimal places and I wanted to use return to let me use i once more in the Main function Basicly can you make...
6
2158
subedimite
by: subedimite | last post by:
I have googled the functionality to use a Browse function to Look- in folders and then select required file from the folder. However when I close the data base and open again, I would like VBA to...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.