473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

initializing float in a class

Hi,

I am trying to initialize a float in a class.

For example:

class ABC{

public:
private:
static const float = 3.3;

};

The compiler does not like that, and I don't know how to declare and define
a float that has class scope only.

Thanks in advance for your help.
Jul 22 '05 #1
5 7234

"john smith" <pr**************@charter.net> wrote in message
news:f0*****************@fe03.lga...
Hi,

I am trying to initialize a float in a class.

For example:

class ABC{

public:
private:
static const float = 3.3;

};

The compiler does not like that, and I don't know how to declare and define a float that has class scope only.


Only static constant integer types can be initialized
inside the class body. You also failed to give the
'float' object a name.

class ABC
{
public:

private:
static const float f;
};

const float ABC::f(3.3f);

I recommend preferring type 'double' over 'float' unless
you have a compelling reason to use the latter. 'double'
gives you much better precision.

-Mike
Jul 22 '05 #2
john smith wrote:
Hi,

I am trying to initialize a float in a class.

For example:

class ABC{

public:
private:
static const float = 3.3;
'static const float' is a type but you have no identifier.

Anyway I think only integrals are allowed for 'static const'.

};

The compiler does not like that, and I don't know how to declare and define
a float that has class scope only.

Thanks in advance for your help.


--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #3
john smith wrote:
Hi,

I am trying to initialize a float in a class.

For example:

class ABC{

public:
private:

You forgot an identifier.

static const float x = 3.3;
};


However are you sure about the private part?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
Mike Hewson wrote:
Anyway I think only integrals are allowed for 'static const'.

Yes you are right. Here is also what VC++ 2003 and 2005 Beta say about it:
C:\c>cl /clr temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 13.10.3077 for .NET
Framework
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

temp.cpp
temp.cpp(4) : error C2864: 'x' : only const static integral data members
can be
initialized inside a class or struct

C:\c>
C:\c>cl /clr temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40904
for Microsoft (R) .NET Framework version 2.00.40607.16
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
temp.cpp(4) : error C2864: 'ABC::x' : only static const integral data
members ca
n be initialized within a class

C:\c>


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
if you need a const static float attribute in your class,
initialize it in the class's implementation file:

const float ClassName::floatAttributeName = 2.2f;

On Tue, 4 Jan 2005 19:11:39 -0800, john smith
<pr**************@charter.net> wrote:
Hi,

I am trying to initialize a float in a class.

For example:

class ABC{

public:
private:
static const float = 3.3;

};

The compiler does not like that, and I don't know how to declare and
define
a float that has class scope only.

Thanks in advance for your help.


--
have a nice day
ulrich
Jul 22 '05 #6

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
14
by: Avi Uziel | last post by:
Hi All, I'm writing a Windows DLL which contain some utility classes. One of my classes is Singleton, therefore contain some static members. I'm using VC6 and linking to the DLL statically. ...
2
by: J.J. Feminella | last post by:
I have source code similar to the following: public class Vehicle { protected int dataA; // ... additional fields } public class FourWheelVehicle : Vehicle {
0
by: Terry Reedy | last post by:
Prashant Saxena wrote: A property with a working get and set that raises an exception. Don't know. I believe this is what slots is for.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.