Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I got strange resule while compiling this code...

Reply
Thread Tools

I got strange resule while compiling this code...

 
 
=?gb2312?B?tdjH8reitq+7+g==?=
Guest
Posts: n/a
 
      04-07-2007
Who can tell me the result in your compiler? Is it a bug or standard
beheiver?
class A
{
template<int>
void a();

};

class B:A
{

};

template<>
void B::a<0>()
{
B *b=this; // comment this line!

}

And what would happen when you comment the marked line?

(I got some stupid result in my compiler... Who can answer that is not
a bug of the compiler? I will show you the stupid answer after some
replies. )

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-07-2007
地球发动机 wrote:

> Who can tell me the result in your compiler?


Well, in my compiler, the result is:

error: template-id 'a<0>' for 'void B::a()' does not match any template
declaration
error: invalid function declaration

which is pretty much what I would have expected.


> Is it a bug or standard
> beheiver?
> class A
> {
> template<int>
> void a();
>
> };
>
> class B:A
> {
>
> };
>
> template<>
> void B::a<0>()
> {
> B *b=this; // comment this line!
>
> }
>
> And what would happen when you comment the marked line?


Exactly the same.

> (I got some stupid result in my compiler... Who can answer that is not
> a bug of the compiler?


I don't know if "some stupid result" is a bug, since you didn't tell what
you mean by that.

> I will show you the stupid answer after some replies. )


 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      04-07-2007
On Apr 7, 12:03 pm, "地球发动机" <(E-Mail Removed)> wrote:
> Who can tell me the result in your compiler? Is it a bug or standard
> beheiver?
> class A
> {
> template<int>
> void a();


the templated function is private.

>
> };
>
> class B:A
> {
>
> };


private inheritence

>
> template<>
> void B::a<0>()
> {
> B *b=this; // comment this line!
>
> }


B::a< int >() was never declared, only A::a< int >() was

>
> And what would happen when you comment the marked line?


what for - the above won't compile

>
> (I got some stupid result in my compiler... Who can answer that is not
> a bug of the compiler? I will show you the stupid answer after some
> replies. )


answer?
Since you haven't asked any question that makes sense yet, take the
following and try asking again...

#include<iostream>

class A
{
public:
template< const int N >
void a() const;
};

template< const int N >
void A::a() const
{
std::cout << "N = " << N << std::endl;
std::cout << this << std::endl;
}

class B : public A
{
};

int main() {
B b;
b.a< 0 >();
}


 
Reply With Quote
 
=?gb2312?B?tdjH8reitq+7+g==?=
Guest
Posts: n/a
 
      04-08-2007
On 48, 12ʱ54, Rolf Magnus <(E-Mail Removed)> wrote:
> 򷢶 wrote:
> > Who can tell me the result in your compiler?

>
> Well, in my compiler, the result is:
>
> error: template-id 'a<0>' for 'void B::a()' does not match any template
> declaration
> error: invalid function declaration
>
> which is pretty much what I would have expected.
>
>
>
>
>
> > Is it a bug or standard
> > beheiver?
> > class A
> > {
> > template<int>
> > void a();

>
> > };

>
> > class B:A
> > {

>
> > };

>
> > template<>
> > void B::a<0>()
> > {
> > B *b=this; // comment this line!

>
> > }

>
> > And what would happen when you comment the marked line?

>
> Exactly the same.
>
> > (Igotsome stupid result in my compiler... Who can answer that is not
> > a bug of the compiler?

>
> I don't know if "some stupid result" is a bug, since you didn't tell what
> you mean by that.
>
>
>
> > I will show you the stupid answer after some replies. )- ر -

>
> - ʾõ -- ر -
>
> - ʾõ -


Good, now I am sure that I got from my compiler - Visual C++ 2005 - is
a bug, because the result is:

error C2440: 'initializing' : cannot convert from 'A *const ' to 'B *'
Cast from base to derived requires dynamic_cast or static_cast

and if I comment the line inside B::a<0>(), everything ok.

It seems that the compiler consider that the B::a<0>() to be A::a<0>().

 
Reply With Quote
 
=?gb2312?B?tdjH8reitq+7+g==?=
Guest
Posts: n/a
 
      04-08-2007
On 48, 3ʱ05, "Salt_Peter" <pj_h...@yahoo..com> wrote:
> On Apr 7, 12:03 pm, "򷢶" <(E-Mail Removed)> wrote:
>
> > Who can tell me the result in your compiler? Is it a bug or standard
> > beheiver?
> > class A
> > {
> > template<int>
> > void a();

>
> the templated function is private.
>
>
>
> > };

>
> > class B:A
> > {

>
> > };

>
> private inheritence
>
>
>
> > template<>
> > void B::a<0>()
> > {
> > B *b=this; // comment this line!

>
> > }

>
> B::a< int >() was never declared, only A::a< int >() was
>
>
>
> > And what would happen when you comment the marked line?

>
> what for - the above won't compile
>
>
>
> > (I got some stupid result in my compiler... Who can answer that is not
> > a bug of the compiler? I will show you the stupid answer after some
> > replies. )

>
> answer?
> Since you haven't asked any question that makes sense yet, take the
> following and try asking again...
>
> #include<iostream>
>
> class A
> {
> public:
> template< const int N >
> void a() const;
>
> };
>
> template< const int N >
> void A::a() const
> {
> std::cout << "N = " << N << std::endl;
> std::cout << this << std::endl;
>
> }
>
> class B : public A
> {
>
> };
>
> int main() {
> B b;
> b.a< 0 >();
>
>
>
> }- ر -
>
> - ʾõ -


Your modify is correct and perfect. But I just talking about a stupid
bug of my compiler(VC2005). And now I believe it is not my
misunderstand of the language.

My compiler consider the error of the code is that I cast the 'this'
pointer to B * from A *(I don't know why it consider the this pointer
inside B::a() is a B *). And if I comments the line of casting, it can
be compiled without event a warning!

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      04-08-2007
On Apr 7, 9:34 pm, "地球发动机" <(E-Mail Removed)> wrote:
> On 4月8日, 上午3时05分, "Salt_Peter" <(E-Mail Removed)> wrote:
>
>
>
> > On Apr 7, 12:03 pm, "地球发动机" <(E-Mail Removed)> wrote:

>
> > > Who can tell me the result in your compiler? Is it a bug or standard
> > > beheiver?
> > > class A
> > > {
> > > template<int>
> > > void a();

>
> > the templated function is private.

>
> > > };

>
> > > class B:A
> > > {

>
> > > };

>
> > private inheritence

>
> > > template<>
> > > void B::a<0>()
> > > {
> > > B *b=this; // comment this line!

>
> > > }

>
> > B::a< int >() was never declared, only A::a< int >() was

>
> > > And what would happen when you comment the marked line?

>
> > what for - the above won't compile

>
> > > (I got some stupid result in my compiler... Who can answer that is not
> > > a bug of the compiler? I will show you the stupid answer after some
> > > replies. )

>
> > answer?
> > Since you haven't asked any question that makes sense yet, take the
> > following and try asking again...

>
> > #include<iostream>

>
> > class A
> > {
> > public:
> > template< const int N >
> > void a() const;

>
> > };

>
> > template< const int N >
> > void A::a() const
> > {
> > std::cout << "N = " << N << std::endl;
> > std::cout << this << std::endl;

>
> > }

>
> > class B : public A
> > {

>
> > };

>
> > int main() {
> > B b;
> > b.a< 0 >();

>
> > }- 隐藏被引用文* -

>
> > - 显示引用的文* -

>
> Your modify is correct and perfect. But I just talking about a stupid
> bug of my compiler(VC2005). And now I believe it is not my
> misunderstand of the language.
>
> My compiler consider the error of the code is that I cast the 'this'
> pointer to B * from A *(I don't know why it consider the this pointer
> inside B::a() is a B *). And if I comments the line of casting, it can
> be compiled without event a warning!


Your code compiled without a warning? i doubt that emphatically.
Let me put it to you this way: in your code: A::a() was declared but
not defined
and B::a() does not exist. It was never declared in class B.

Obviously if you declared and then defined both A::a() and B::a()
properly,
the this pointer would have to be a B* in B::a().
I don't see why this fact puzzles you?


 
Reply With Quote
 
=?gb2312?B?tdjH8reitq+7+g==?=
Guest
Posts: n/a
 
      04-08-2007
On 48, 1ʱ58, "Salt_Peter" <pj_h...@yahoo..com> wrote:
> On Apr 7, 9:34 pm, "򷢶" <(E-Mail Removed)> wrote:
>
>
>
> > On 48, 3ʱ05, "Salt_Peter" <(E-Mail Removed)> wrote:

>
> > > On Apr 7, 12:03 pm, "򷢶" <(E-Mail Removed)> wrote:

>
> > > > Who can tell me the result in your compiler? Is it a bug or standard
> > > > beheiver?
> > > > class A
> > > > {
> > > > template<int>
> > > > void a();

>
> > > the templated function is private.

>
> > > > };

>
> > > > class B:A
> > > > {

>
> > > > };

>
> > > private inheritence

>
> > > > template<>
> > > > void B::a<0>()
> > > > {
> > > > B *b=this; // comment this line!

>
> > > > }

>
> > > B::a< int >() was never declared, only A::a< int >() was

>
> > > > And what would happen when you comment the marked line?

>
> > > what for - the above won't compile

>
> > > > (I got some stupid result in my compiler... Who can answer that is not
> > > > a bug of the compiler? I will show you the stupid answer after some
> > > > replies. )

>
> > > answer?
> > > Since you haven't asked any question that makes sense yet, take the
> > > following and try asking again...

>
> > > #include<iostream>

>
> > > class A
> > > {
> > > public:
> > > template< const int N >
> > > void a() const;

>
> > > };

>
> > > template< const int N >
> > > void A::a() const
> > > {
> > > std::cout << "N = " << N << std::endl;
> > > std::cout << this << std::endl;

>
> > > }

>
> > > class B : public A
> > > {

>
> > > };

>
> > > int main() {
> > > B b;
> > > b.a< 0 >();

>
> > > }- ر -

>
> > > - ʾõ -

>
> > Your modify is correct and perfect. But I just talking about a stupid
> > bug of my compiler(VC2005). And now I believe it is not my
> > misunderstand of the language.

>
> > My compiler consider the error of the code is that I cast the 'this'
> > pointer to B * from A *(I don't know why it consider the this pointer
> > inside B::a() is a B *). And if I comments the line of casting, it can
> > be compiled without event a warning!

>
> Your code compiled without a warning? i doubt that emphatically.
> Let me put it to you this way: in your code: A::a() was declared but
> not defined
> and B::a() does not exist. It was never declared in class B.
>
> Obviously if you declared and then defined both A::a() and B::a()
> properly,
> the this pointer would have to be a B* in B::a().
> I don't see why this fact puzzles you?


I am sorry I have make a mistake. "I don't know why it consider the
this
pointer inside B::a() is a B *" should be "I don't know why it
consider
the this pointer inside B::a() is an A *". And it it is that I puzzles
for.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 09:47 PM
Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 08:55 PM
While deploying application on AIX I got this error. while it's working fine on windows Sam Java 0 06-21-2006 06:26 AM
Strange error while compiling a C++ file Arnab Nandi C++ 5 08-21-2004 09:07 AM
Got error msg while Debugging : Error while trying to run project: ... ^CrazyCoder^ ASP .Net 3 09-15-2003 09:40 AM



Advertisments