Connecting Tech Pros Worldwide Help | Site Map

Help. const object uninitialized problem.

  #1  
Old July 23rd, 2005, 05:52 AM
nan.li.g@gmail.com
Guest
 
Posts: n/a
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.

  #2  
Old July 23rd, 2005, 05:52 AM
Alf P. Steinbach
Guest
 
Posts: n/a

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?
  #3  
Old July 23rd, 2005, 05:53 AM
nan.li.g@gmail.com
Guest
 
Posts: n/a

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 Threads
Thread Thread Starter Forum Replies Last Post
problem need help Oliver Bleckmann answers 8 November 15th, 2006 08:55 PM
error: uninitialized reference member supachamp@gmx.net answers 7 November 22nd, 2005 04:12 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM
Urgent Help, some doubts Piotre Ugrumov answers 1 July 22nd, 2005 06:41 AM