473,414 Members | 1,703 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,414 software developers and data experts.

Help. const object uninitialized problem.

I have this simple code below. When I compiled it, I got the following
error. But after I removed the comment marker(//), i.e. explicitly
defined a constructor, it becomes OK.

The compiler should generate a default constructor for me and the
default one shoule be no different than the one I specified. Why do I
have to specify one in this code?

class A
{
public:
//A() { }
};
int main()
{
const A a;
return 0;
}
[nan@athena test]$ g++ test19.cpp
test19.cpp: In function `int main()':
test19.cpp:10: error: uninitialized const `a'
Thank you very much.

Jul 23 '05 #1
2 2993
* na******@gmail.com:

class A
{
public:
//A() { }
};
int main()
{
const A a;
return 0;
}

The compiler should generate a default constructor for me and the
default one shoule be no different than the one I specified. Why do I
have to specify one in this code?
Look at the error message which you have repeated as subject line.

It's not meaningful to have a constant without a specified value.

You're lucky: Visual C++ 7.1 erronously compiles the above without
flagging the error.

[nan@athena test]$ g++ test19.cpp
test19.cpp: In function `int main()':
test19.cpp:10: error: uninitialized const `a'


In the code as-is no value is provided for the constant, and
according to §8.5/9 the program is "ill-formed".

You can (1) initialize explicitly, e.g..

A const a = {};

(allowed only for aggregate type, which A is) or

A const a = A();

Or you can (2) define your own default constructor.

Any way you need to explicitly define the value of the constant.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Thank you. Your answer is very clear.

Alf P. Steinbach wrote:
* na******@gmail.com:

class A
{
public:
//A() { }
};
int main()
{
const A a;
return 0;
}

The compiler should generate a default constructor for me and the
default one shoule be no different than the one I specified. Why do I
have to specify one in this code?


Look at the error message which you have repeated as subject line.

It's not meaningful to have a constant without a specified value.

You're lucky: Visual C++ 7.1 erronously compiles the above without
flagging the error.

[nan@athena test]$ g++ test19.cpp
test19.cpp: In function `int main()':
test19.cpp:10: error: uninitialized const `a'


In the code as-is no value is provided for the constant, and
according to §8.5/9 the program is "ill-formed".

You can (1) initialize explicitly, e.g..

A const a = {};

(allowed only for aggregate type, which A is) or

A const a = A();

Or you can (2) define your own default constructor.

Any way you need to explicitly define the value of the constant.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jul 23 '05 #3

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

Similar topics

4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
1
by: Piotre Ugrumov | last post by:
I have some problems and some doubts. I have implemented a class hierachy. The base class Velivolo, from Velivolo derive Militare and Civile, from militare derive Aereo and Elicottero, from Civile...
4
by: BigMan | last post by:
Why does an emtpy class require an explicit default ctor in order to initialize const objects of that type: class EmptyClass { }; class EmptyClass2 { EmtpyClass2( ) { }
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
1
by: siliconwafer | last post by:
Hi All, If I declare an identifier with a const keyword and not initilize it.Then later on I assign it value of an expression.Is it fine? I feel yes.But if I modify the value of that constant like...
8
by: Oliver Bleckmann | last post by:
i want to create a object of class Hotel, which own a variable number of objects of the class Zimmer (~ rooms). accessed in main like this Hotel hotel("name",9); std::cout <<...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
13
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their...
8
by: zl2k | last post by:
hi, there Here is a simplified piece of code of my program, it compiles and runs fine. However, valgrind shows it has uninitialized problem. What I am doing wrong? #ifndef DATA2_H #define...
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...
0
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,...
0
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...
0
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
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...
0
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
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
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...

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.