473,624 Members | 2,601 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<iostre am.h>
#include<math.h >
#include<conio. h>
class legendre
{
private:
int c,b,z,zz,xx,yy, fact;
double a,nt,x,y,aa,fin al,bb,cc;
double sum1=0;
public:
int factorial(int n)
{
if (n==0)
return 1;
else
{
fact=n*factoria l(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*(po w(cos(theta),b-2*q)));
}

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

void main()
{
int l,m;
double theta;
cout<<"enter the values for l, m and theta: "<<endl;
cin>>l>>m>>thet a;
legendre expression;
expression.calc ulate(l,m,theta );
getch();
}
Jul 19 '05 #1
3 3220

"Shashidhar Patil" <hi******@yahoo .com> wrote in message
news:fb******** *************** *@posting.googl e.com...
here is the code

#include<iostre am.h>
#include<math.h >
#include<conio. h>
class legendre
{
private:
int c,b,z,zz,xx,yy, fact;
double a,nt,x,y,aa,fin al,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<iostre am.h>
#include<math.h >
#include<conio. h>
class legendre
{
private:
int c,b,z,zz,xx,yy, fact;
double a,nt,x,y,aa,fin al,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*factoria l(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),fi nal(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*(po w(cos(theta),b-2*q)));
}

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

[snip]
Jul 19 '05 #3
Shashidhar Patil wrote:
here is the code

#include<iostre am.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,fin al,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>>thet a;
legendre expression;
expression.calc ulate(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
3515
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 { lengthSV=16 }; // Length of SomeVector vector<double> SomeVector;
6
2782
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
26471
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 < N; i++)
1
1938
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
4898
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
1011
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; 004c83E3 fldz 004c83E9 fstp qword ptr double dMaxVal = 0.99;
1
1908
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 ) { fib = num1 + num2;
3
5995
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 return work with a double variable
6
2162
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 remember the path name, so that I don’t have to Browse the folder and look for the same files again. I suppose, some how I need to initialize a global variable with the value how it exited the database previously. Does this sound right? What is...
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8348
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8493
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1493
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.