Help. const object uninitialized problem.

N

nan.li.g

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
N

nan.li.g

Thank you. Your answer is very clear.
* (e-mail address removed):
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top