473,830 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7260

"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::floa tAttributeName = 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
6396
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
14
10651
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. My question is where is the correct (and best way) to initialize theses variables? If I initialize them in the class CPP (in the DLL) then I get linkage error when I try to link my project with the LIB, and I don't want to make the user initialize...
2
269
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
872
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
9793
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
9642
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
10774
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...
0
10202
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
9314
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
7746
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
6950
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
3959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
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.