Connecting Tech Pros Worldwide Help | Site Map

Help. const object uninitialized problem.

nan.li.g@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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.

Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Help. const object uninitialized problem.


* nan.li.g@gmail.com:[color=blue]
>
> 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?[/color]

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.

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

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?
nan.li.g@gmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Help. const object uninitialized problem.


Thank you. Your answer is very clear.

Alf P. Steinbach wrote:[color=blue]
> * nan.li.g@gmail.com:[color=green]
> >
> > 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?[/color]
>
> 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.
>
>[color=green]
> > [nan@athena test]$ g++ test19.cpp
> > test19.cpp: In function `int main()':
> > test19.cpp:10: error: uninitialized const `a'[/color]
>
> 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?[/color]

Closed Thread


Similar C / C++ bytes