redefinition of formal parameter

  • Follow


Howdy all, I am attempting to write a try.throw.catch block, and I have

to logic of it down, but I am hitting a syntax snag with my char *:


in my driver:
void test(int x, int y)
{
    if (x >= y)
    throw errorHandle("smaller was larger than bigger");



}


in class "errorHandle":
//constructor
errorHandle(char *x)
{
     exception(x);


}


and i get:
Redifinition of formal parameter 'x';

any clues?


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply MikeGeig 9/26/2005 8:27:39 AM

On 26 Sep 2005 04:27:39 -0400, "MikeGeig@gmail.com" <MikeGeig@gmail.com>
wrote:

> in my driver:
> void test(int x, int y)
> {
>     if (x >= y)
>     throw errorHandle("smaller was larger than bigger");
> }

> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>      exception(x);
> }

> and i get:
> Redifinition of formal parameter 'x';

You have a formal parameter char* x and are declaring a local variable
(std::)exception x.  The parens are redundant.  Since this is a
constructor, I assume the class derives from std::exception and that you
intended to initialize the base.

   errorHandle (char* x) : exception(x) { }

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply John 9/27/2005 11:20:39 AM


MikeGeig@gmail.com wrote:
> Howdy all, I am attempting to write a try.throw.catch block, and I have
>
> to logic of it down, but I am hitting a syntax snag with my char *:
>
>
> in my driver:
> void test(int x, int y)
> {
>     if (x >= y)
>     throw errorHandle("smaller was larger than bigger");
>
>
>
> }
>
>
> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>      exception(x);
>
>
> }
>
>
> and i get:
> Redifinition of formal parameter 'x';
>
> any clues?

I presume in errorHandle somewhere you have
#include <stdexcept>
using namespace std;

the line
    exception(x);

is being treated as a declaration, such as
    exception x;

It surprises me slightly that this is treated as an error, as the I
thought {} denote a new context.

Are you trying to do "throw x" (stylistically bad, as x is a char *),
or possibly throw std::runtime_error(x).

If you had a really pernickety compiler, you'd also be getting a
warning that you were passing a string (generally char const *) to a
char * parameter, which is subtly dangerious.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ThosRTanner 9/27/2005 11:30:10 AM

hi mike,


plz have the solution below


class errorHandle{//constructor
public:
errorHandle(char *x){
      exception a(x); //create am object of name other than x
}
};


void test(int x, int y){
    if (x >= y)
    throw errorHandle("smaller was larger than bigger");
}

BTW you cannot pass string constatns the way you are trying to pass.
this likely to cause runtime error when you are going to use this data.


thanks
rt


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ravinderthakur 9/27/2005 11:31:48 AM

MikeGeig@gmail.com wrote:
> Howdy all, I am attempting to write a try.throw.catch block, and I have
> 
> to logic of it down, but I am hitting a syntax snag with my char *:
> 
> 
> in my driver:
> void test(int x, int y)
> {
>     if (x >= y)
>     throw errorHandle("smaller was larger than bigger");
> 
> 
> 
> }
> 
> 
> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>      exception(x);
> 
> 
> }

Did you mean to write

    errorHandle(char const * x) : std::exception(x) {}

???

> 
> 
> and i get:
> Redifinition of formal parameter 'x';
> 
> any clues?

Not really, without seeing more of 'errorHandle' class definition...

V

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Victor 9/27/2005 11:33:32 AM

<MikeGeig@gmail.com> schrieb im Newsbeitrag 
news:1127698815.923031.118000@o13g2000cwo.googlegroups.com...
> Howdy all, I am attempting to write a try.throw.catch block, and I have
>
> to logic of it down, but I am hitting a syntax snag with my char *:
>
>
> in my driver:
> void test(int x, int y)
> {
>    if (x >= y)
>    throw errorHandle("smaller was larger than bigger");
>
>
>
> }
>
>
> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>     exception(x);
>
>
> }
>
>
> and i get:
> Redifinition of formal parameter 'x';
>
> any clues?

Some compiler accept "smaller was larger than bigger" as char * other as 
const char *.
It seems you have second one. Try to use: errorHandle(const char *x) and
throw errorHandle((const char*)"smaller was larger than bigger")




      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply romsky 9/27/2005 2:27:33 PM

MikeGeig@gmail.com wrote:

[snip]
>
> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>      exception(x);
>
>
> }
>
>
> and i get:
> Redifinition of formal parameter 'x';

You can get this error if 'exception' is a macro and expands to code
which also contains a definition of 'x'. Since, you cannot have
multiple definitions of the same variable within the same scope, the
compiler generates the error.

BTW, you may want to change the type of 'x' in the constructor to
"const char *x".

Best Regards,
Murali Desikan


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Murali 9/27/2005 2:30:24 PM

MikeGeig@gmail.com wrote:
> //constructor
> errorHandle(char *x)
> {
>      exception(x);
> }
> 
> and i get:
> Redifinition of formal parameter 'x';
> 
> any clues?
> 

Because "exception(x);" inside the body of a function is a declaration
of a new variable x with type exception. Yes, the parentheses are
completely ignored in this case. What you are probably trying to write
is a ctor-initializer, which has this syntax instead:

errorHandle(char *x)
: exception(x)
{}

BTW, you should declare x as const char* instead of char*, because
otherwise the code won't compile, as the constructor of class
std::exception expects a const char* and there's no conversion from
char* to const char*.

Remember that a string literal is an array of const char, so it
naturally decays to const char* not char*. The C++ standard still
allows, as a special case, the conversion from a string literal to
char*, but the conversion is deprecated and it's bad programming style
to rely on that, IMHO.

HTH,

Ganesh

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Alberto 9/27/2005 2:30:53 PM

This code is incomplete. Please, give minimal fragment to reproduce the
error. What does �exception� name mean? There is its declaration?


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply tpochep 9/27/2005 2:31:20 PM

Please post the class and especially the constructor declaration.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply unspammable 9/27/2005 2:33:50 PM

[Next time please post the minimal program that allows your audience
to see what you are seeing.]

"MikeGeig@gmail.com" <MikeGeig@gmail.com> writes:

> Howdy all, I am attempting to write a try.throw.catch block, and I have
> to logic of it down, but I am hitting a syntax snag with my char *:
>
>
> in my driver:
> void test(int x, int y)
> {
>     if (x >= y)
>       throw errorHandle("smaller was larger than bigger");
> }
>
>
> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>      exception(x);

This looks like the definition of a local variable x.

> }

Did you mean to write:

errorHandle::errorHandle(char *x)
: exception(x)
{
}

?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Thomas 9/27/2005 2:34:11 PM

Hi Mike,

MikeGeig@gmail.com wrote:
> in class "errorHandle":
> //constructor
> errorHandle(char *x)
> {
>      exception(x);
> 
> 
> }
> 
> and i get:
> Redifinition of formal parameter 'x';

It would help if you provide the class declaration or even what
'exception' is. You handle it if it were a function taking a char* as
parameter.

Assuming it is a char* member variable to store the error string, you
could assign:

exception = x;

Or initialize it (noted before the open bracket):

errorHandle(char *x) : exception(x)
{
}

Yet better change to const char*.

Thomas

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Thomas 9/27/2005 2:52:11 PM

ravinderthakur@gmail.com wrote:

> void test(int x, int y){
>     if (x >= y)
>     throw errorHandle("smaller was larger than bigger");
> }
>
> BTW you cannot pass string constatns the way you are trying to pass.

You certainly can.

> this likely to cause runtime error when you are going to use this
> data.

This is perfectly safe as long as you limit yourself to *reading* the 
data. String literals are read-only data that are available for the 
entire lifetime of the program. It is only when you try to *write* to 
them that you enter the land of undefined behaviour. Sadly, this is made 
easy through the C compatibility hack that converts string literals 
implicitly from array of const char to char*.

-- 
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address 
with "kapsch" and the top level domain part with "net".

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Gerhard 9/27/2005 3:07:08 PM

Hi,

Please ignore my previous post below.

Regards,
Murali

Murali wrote:
> MikeGeig@gmail.com wrote:
>
> [snip]
> >
> > in class "errorHandle":
> > //constructor
> > errorHandle(char *x)
> > {
> >      exception(x);
> >
> >
> > }
> >
> >
> > and i get:
> > Redifinition of formal parameter 'x';
>
> You can get this error if 'exception' is a macro and expands to code
> which also contains a definition of 'x'. Since, you cannot have
> multiple definitions of the same variable within the same scope, the
> compiler generates the error.
>
> BTW, you may want to change the type of 'x' in the constructor to
> "const char *x".
>
> Best Regards,
> Murali Desikan

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Murali 9/28/2005 2:35:42 AM

13 Replies
480 Views

(page loaded in 0.155 seconds)

Similiar Articles:










7/22/2012 1:59:05 PM


Reply: