D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] A programming problem



On 7 May 2003, James Ogden wrote:

> On Wed, 2003-05-07 at 21:26, Tom Glare wrote:
> > Are there any Charlie Double Plus programmers who could explain this
> > little conundrum to me ?
> >
> > When I compile the following programme
> >
> > ===
> >
> > int main() {
> >    int * a = new int;
> >    int b;
> >    switch (b) {
> >       case 0 :
> >          int * c = new int;
> >          // int * c;                  Compiles sucessfully
> >          // c = new int;              if I do it this way
> >          break;
> >       case 1 :
> >          int * d = new int;
> >          break;
> >    }
> >    return 0;
> > }
> > ===
> > - I get the following error :
> > ===
> >
> > test.cpp: In function `int main ()':
> > test.cpp:10: jump to case label
> > test.cpp:6:   crosses initialization of `int *c'
> >
> > ===
> >
> > Am relatively new to the wonderful world of C++, so is probably something
> > elementary I am getting wrong.
> >
>
> When I use g++ test.cpp, an a.out gets compiled for both of the program
> versions.
>
> When I use gcc test.cpp, things go a little odd + create the results you
> report, I may be wrong but I would say you are compiling C++ with a
> confused compiler ?
>
> As a matter of interest, are you going to initialise the value for b?  I
> assume that you have used this simple case as an example of your
> problem.
>
The plot thickens !  One would think that if the compiler was going to
complain, it would pick on the other two lines which do the same thing.
But it only happens in the switch statement. "g++ -v" gives me "gcc
version 2.96".  Yes, ignore the silly program - the problem came up in a
much more complicated program - this example was just the simplest I could
come up with to illustrate the problem.

Cheers, Tom.


--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly