473,732 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3017
* 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
2758
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 hope that someone could help me out. As said in an earlier post I am new to c++, this is alot harder to do than VB. I am for the most part self taught with what I already know and looking to learn more. The book I am using to teach myself from...
1
1496
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 derive Passeggero e Merce. In every class I have implemented the overload of << and >>. In a class Simulator (not in hierachy) I have implemented the overload of << in this way: ostream &operator<<(ostream &out, const Simulatore &s){...
4
1723
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
8259
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 generic pointer type. My real question is, is (void *) such a generic pointer type that it
1
1153
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 say an operation like, const int i; int a,b; i = a + b; ..... i++;
8
1459
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 << hotel.zimmers->getZimmerZahl(); //(~ hotel.rooms->getRoomNumber) well, i don't know where to put the pointer declaration. Zimmer* zimmers; // Array als Pointer auf Objekte in the constructor
19
1961
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 percentage of the student by the formula (Ob_marks * 100 ) / Total_marks and Display() which show all information of the student. The class should also contain the default constructor which initializes all the data member of the class. In main program...
13
2841
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 values are determined during the execution of the initialization routines, so I made them const (I'd rather making it hard for a dev to accidentally overwrite the values). The problem is, they are being set run-time by the return of a function...
8
1865
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 DATA2_H class Data2{ public:
0
8944
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
8773
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
9445
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
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
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...
0
6030
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();...
0
4548
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.