Connecting Tech Pros Worldwide Help | Site Map

Declaration error

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 4th, 2007, 12:55 PM
Pierre Couderc
Guest
 
Posts: n/a
Default Declaration error

What do I do wrong?

class CTest
{
public:
CTest(int uu);
};

void ttt()
{
int uu=88;
CTest(uu);
CTest(88);
}


I get 2 errors that I do not understand on CTest(uu) with MSVC6 :
error C2371: 'uu' : redefinition; different basic types
error C2512: 'CTest' : no appropriate default constructor available

I have a way CTest((int) uu); to get it working, but I would like to
understand where is the problem...

Thank you in advance

Pierre Couderc

  #2  
Old January 4th, 2007, 01:25 PM
Bernhard Berger
Guest
 
Posts: n/a
Default Re: Declaration error

Hi,
Quote:
What do I do wrong?
You forgot the names of the variables.
Quote:
class CTest
{
public:
CTest(int uu);
};
>
void ttt()
{
int uu=88;
CTest foo(uu);
CTest bar(88);
}
This one should work.

Bernhard 'berber' Berger
  #3  
Old January 4th, 2007, 01:25 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Declaration error

Pierre Couderc wrote:
Quote:
What do I do wrong?
>
class CTest
{
public:
CTest(int uu);
};
>
void ttt()
{
int uu=88;
CTest(uu);
CTest(88);
}
>
>
I get 2 errors that I do not understand on CTest(uu) with MSVC6 :
error C2371: 'uu' : redefinition; different basic types
error C2512: 'CTest' : no appropriate default constructor available
In
CText(uu);

the parens are superfluous. It means the same as

CText uu;

so in your first line in main, you define an int named uu, and in the next
line, a definition of a default-contructed CText with the same name
follows.

  #4  
Old January 4th, 2007, 01:25 PM
mlimber
Guest
 
Posts: n/a
Default Re: Declaration error

Pierre Couderc wrote:
Quote:
What do I do wrong?
>
class CTest
{
public:
CTest(int uu);
};
>
void ttt()
{
int uu=88;
CTest(uu);
CTest(88);
}
>
>
I get 2 errors that I do not understand on CTest(uu) with MSVC6 :
error C2371: 'uu' : redefinition; different basic types
error C2512: 'CTest' : no appropriate default constructor available
>
I have a way CTest((int) uu); to get it working, but I would like to
understand where is the problem...
You forgot the object name, and the compiler thinks you're declaring a
CTest object by the name of uu, but there is already an int by that
name in the current scope. Try:

CTest c( uu ); // Works fine

This problem is similar to the one described in this FAQ:

http://parashift.com/c++-faq-lite/ctors.html#faq-10.19

Cheers! --M

  #5  
Old January 4th, 2007, 01:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Declaration error

Bernhard Berger wrote:
Quote:
Hi,
>
Quote:
>What do I do wrong?
You forgot the names of the variables.
>
Quote:
>class CTest
>{
>public:
>CTest(int uu);
>};
>>
>void ttt()
>{
>int uu=88;
>CTest foo(uu);
>CTest bar(88);
>}
This one should work.
>
Bernhard 'berber' Berger
Just to suggest something on posting practices: NEVER correct
the posting to which you respond by changing the text which you
quote. ALWAYS place your corrections after the text which you
are correcting. Do it like this:

[original text, which you quote:]
Quote:
int uu=88;
CTest(uu);
CTest(88);
[your correction:]
CTest foo(uu);
CTest bar(88);

Otherwise it seems that the original poster (whose message you
quoted) did everything right in the first place.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #6  
Old January 4th, 2007, 02:05 PM
Pierre Couderc
Guest
 
Posts: n/a
Default Re: Declaration error

Pierre Couderc a écrit :
Quote:
What do I do wrong?
>
class CTest
{
public:
CTest(int uu);
};
>
void ttt()
{
int uu=88;
CTest(uu);
CTest(88);
}
>
>
I get 2 errors that I do not understand on CTest(uu) with MSVC6 :
error C2371: 'uu' : redefinition; different basic types
error C2512: 'CTest' : no appropriate default constructor available
>
I have a way CTest((int) uu); to get it working, but I would like to
understand where is the problem...
>
Thank you in advance
>
Pierre Couderc
My (bad)idea was that it was not necessary to declare an explicit name
as the constructor of CTest makes all the work....


Thank you all.
Pierre Couderc
  #7  
Old January 4th, 2007, 02:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Declaration error

Pierre Couderc wrote:
Quote:
Pierre Couderc a écrit :
Quote:
>What do I do wrong?
>>
>class CTest
>{
>public:
> CTest(int uu);
>};
>>
>void ttt()
>{
> int uu=88;
> CTest(uu);
> CTest(88);
>}
>>
>>
>I get 2 errors that I do not understand on CTest(uu) with MSVC6 :
>error C2371: 'uu' : redefinition; different basic types
>error C2512: 'CTest' : no appropriate default constructor available
>>
>I have a way CTest((int) uu); to get it working, but I would like to
>understand where is the problem...
>>
>Thank you in advance
>>
>Pierre Couderc
>
My (bad)idea was that it was not necessary to declare an explicit name
as the constructor of CTest makes all the work....
To overcome the "what looks like a declaration is a declaration" issue
in your code, surround the type with parens, not the value:

(CTest)uu;

It stops being a declaration and achieves the same point: constructing
a temporary of type CTest from 'uu'. The compiler can still optimize it
away, though. You should consider a simple stand-alone function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.