C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

redefinition of formal parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
MikeGeig@gmail.com
Guest





PostPosted: Mon Sep 26, 2005 8:27 am    Post subject: redefinition of formal parameter Reply with quote



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! ]

Back to top
John Potter
Guest





PostPosted: Tue Sep 27, 2005 11:20 am    Post subject: Re: redefinition of formal parameter Reply with quote



On 26 Sep 2005 04:27:39 -0400, "MikeGeig (AT) gmail (DOT) com" <MikeGeig (AT) gmail (DOT) com>
wrote:

Quote:
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! ]


Back to top
ThosRTanner
Guest





PostPosted: Tue Sep 27, 2005 11:30 am    Post subject: Re: redefinition of formal parameter Reply with quote




[email]MikeGeig (AT) gmail (DOT) com[/email] wrote:
Quote:
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! ]


Back to top
ravinderthakur@gmail.com
Guest





PostPosted: Tue Sep 27, 2005 11:31 am    Post subject: Re: redefinition of formal parameter Reply with quote

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! ]

Back to top
Victor Bazarov
Guest





PostPosted: Tue Sep 27, 2005 11:33 am    Post subject: Re: redefinition of formal parameter Reply with quote

[email]MikeGeig (AT) gmail (DOT) com[/email] wrote:
Quote:
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) {}

???

Quote:


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! ]


Back to top
romsky
Guest





PostPosted: Tue Sep 27, 2005 2:27 pm    Post subject: Re: redefinition of formal parameter Reply with quote


<MikeGeig (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1127698815.923031.118000 (AT) o13g2000cwo (DOT) googlegroups.com...
Quote:
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! ]


Back to top
Murali
Guest





PostPosted: Tue Sep 27, 2005 2:30 pm    Post subject: Re: redefinition of formal parameter Reply with quote


[email]MikeGeig (AT) gmail (DOT) com[/email] wrote:

[snip]
Quote:

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! ]


Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue Sep 27, 2005 2:30 pm    Post subject: Re: redefinition of formal parameter Reply with quote

[email]MikeGeig (AT) gmail (DOT) com[/email] wrote:
Quote:
//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! ]


Back to top
tpochep@mail.ru
Guest





PostPosted: Tue Sep 27, 2005 2:31 pm    Post subject: Re: redefinition of formal parameter Reply with quote

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! ]

Back to top
unspammable@gmail.com
Guest





PostPosted: Tue Sep 27, 2005 2:33 pm    Post subject: Re: redefinition of formal parameter Reply with quote

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! ]

Back to top
Thomas Maeder
Guest





PostPosted: Tue Sep 27, 2005 2:34 pm    Post subject: Re: redefinition of formal parameter Reply with quote


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

"MikeGeig (AT) gmail (DOT) com" <MikeGeig (AT) gmail (DOT) com> writes:

Quote:
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.

Quote:
}

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! ]


Back to top
Thomas J. Gritzan
Guest





PostPosted: Tue Sep 27, 2005 2:52 pm    Post subject: Re: redefinition of formal parameter Reply with quote

Hi Mike,

[email]MikeGeig (AT) gmail (DOT) com[/email] wrote:
Quote:
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! ]


Back to top
Gerhard Menzl
Guest





PostPosted: Tue Sep 27, 2005 3:07 pm    Post subject: Re: redefinition of formal parameter Reply with quote

[email]ravinderthakur (AT) gmail (DOT) com[/email] wrote:

Quote:
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.

Quote:
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! ]


Back to top
Murali
Guest





PostPosted: Wed Sep 28, 2005 2:35 am    Post subject: Re: redefinition of formal parameter Reply with quote

Hi,

Please ignore my previous post below.

Regards,
Murali

Murali wrote:
Quote:
MikeGeig (AT) gmail (DOT) 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! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.