473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

I got strange resule while compiling this code...

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. )

Apr 7 '07 #1
6 1603
地球发动机 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. )
Apr 7 '07 #2
On Apr 7, 12:03 pm, "地球发动机" <eartheng...@gmail.comwrote:
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 >();
}
Apr 7 '07 #3
On 48, 12ʱ54, Rolf Magnus <ramag...@t-online.dewrote:
򷢶 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>().

Apr 8 '07 #4
On 48, 3ʱ05, "Salt_Peter" <pj_h...@yahoo..comwrote:
On Apr 7, 12:03 pm, "򷢶" <eartheng...@gmail.comwrote:
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!

Apr 8 '07 #5
On Apr 7, 9:34 pm, "地球发动机" <eartheng...@gmail.comwrote:
On 4月8日, 上午3时05分, "Salt_Peter" <pj_h...@yahoo.comwrote:
On Apr 7, 12:03 pm, "地球发动机" <eartheng...@gmail.comwrote:
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?
Apr 8 '07 #6
On 48, 1ʱ58, "Salt_Peter" <pj_h...@yahoo..comwrote:
On Apr 7, 9:34 pm, "򷢶" <eartheng...@gmail.comwrote:
On 48, 3ʱ05, "Salt_Peter" <pj_h...@yahoo.comwrote:
On Apr 7, 12:03 pm, "򷢶" <eartheng...@gmail.comwrote:
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.

Apr 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Arno R | last post by:
Hi all Yesterday I found a strange corruption-issue that I can't solve yet or actually point my finger at. I converted an A97 app to A2k. I have done this often enough so I didn't expect trouble...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
7
by: Ziggy | last post by:
Just for curiosity. I know that main function is returning an int, but I came cross with the following code. (Unnessecary code is snipped) Is there any problem with it? #include <stdlib.h>...
12
by: unique | last post by:
I have written some programs in c lang yet but today I get confused with output i get. I have function educate(..) which i call in main program this way: J = educate(no_layers, no_neurons,...
6
by: Joe Piscapo | last post by:
Hello, When I compile my program in Visual Studio it does not work for every input, some numbers make it crash. But compiling in gcc works for those numbers that made it crash in Visual Studio. ...
5
by: Nathan Sokalski | last post by:
When I view my index.aspx page any time after the first time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize)...
0
by: han zhiyang | last post by:
I've just studied the "how to" web service and the async pattern in donnet.I make a test with these knowledges,but I got a strange result. Here is my test. 1.Write a simple "Add" service named...
1
by: stromhau | last post by:
Ok, i have a file with main and an additional .cpp file i include in the main file but i get a lot of strange warnings when including. Both files compile just great separately. It seems that it have...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.