473,386 Members | 1,652 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,386 software developers and data experts.

Compiler Problem

I recently decided to do the right thing & write accessor functions for my
class member variables instead of having them public and introduced a rather
nasty bug. The class below shows the error - basically if you forget to add
the parenthesis in the call to function X() then the program compiles ok with
no warnings but gives the wrong answer :(

I'm using VS2005 Prof edition v2.0.50727

The answer seems to be that if you are writing accessor functions then don't
use overloaded functions - maybe use setX & getX instead - or make sure you
don't forget the parenthesis! It seems to think I want a function pointer(?)
but I think it should at least give a warning.

class Test {
public:
Test() : m_Num(10) {}

void X(const double val) {m_Num = val;}

double X(void) {return m_Num;};

private:
double m_Num;
};

int _tmain(int argc, _TCHAR* argv[])
{
Test t;
double y = 5.0;
double z;

// z = t.X // this will generate an error
z = t.X * y; // this will generate no error or warning but won't call t.X()

return 0;
}

I've googled for this but am surprised I've not seen more issues with this
Nov 9 '06 #1
13 1501
murphman wrote:
class Test {
public:
Test() : m_Num(10) {}

void X(const double val) {m_Num = val;}

double X(void) {return m_Num;};

private:
double m_Num;
};

int _tmain(int argc, _TCHAR* argv[])
{
Test t;
double y = 5.0;
double z;

// z = t.X // this will generate an error
z = t.X * y; // this will generate no error or warning but won't call t.X()
This is a compiler bug, this code shouldn't compile, it's definitely an
error, not even a warning. If I remove one of the "X" overloads, I
properly get the error message. I went to dinkumware.com, and tried to
compile it online. The EDG front-end gives an error. Heck, even VC++ 7.1
gives an error. My Borland C++Builder 6.0 doesn't want to compile it
either. No compiler should. The overload somehow confuses VC++ 8.

Can you file a bug report at MS?

P.S. I haven't tried VC++ 2006 (Beta 1). It may be fixed there, I can't
verify it.

Tom
Nov 9 '06 #2
Thanks - I'll file a bug report
Cheers
Joe

"Tamas Demjen" wrote:
murphman wrote:
class Test {
public:
Test() : m_Num(10) {}

void X(const double val) {m_Num = val;}

double X(void) {return m_Num;};

private:
double m_Num;
};

int _tmain(int argc, _TCHAR* argv[])
{
Test t;
double y = 5.0;
double z;

// z = t.X // this will generate an error
z = t.X * y; // this will generate no error or warning but won't call t.X()

This is a compiler bug, this code shouldn't compile, it's definitely an
error, not even a warning. If I remove one of the "X" overloads, I
properly get the error message. I went to dinkumware.com, and tried to
compile it online. The EDG front-end gives an error. Heck, even VC++ 7.1
gives an error. My Borland C++Builder 6.0 doesn't want to compile it
either. No compiler should. The overload somehow confuses VC++ 8.

Can you file a bug report at MS?

P.S. I haven't tried VC++ 2006 (Beta 1). It may be fixed there, I can't
verify it.

Tom
Nov 9 '06 #3
On Thu, 09 Nov 2006 09:19:33 -0800, Tamas Demjen <td*****@yahoo.comwrote:
>murphman wrote:
>class Test {
public:
Test() : m_Num(10) {}

void X(const double val) {m_Num = val;}

double X(void) {return m_Num;};

private:
double m_Num;
};

int _tmain(int argc, _TCHAR* argv[])
{
Test t;
double y = 5.0;
double z;

// z = t.X // this will generate an error
z = t.X * y; // this will generate no error or warning but won't call t.X()

This is a compiler bug, this code shouldn't compile, it's definitely an
error, not even a warning. If I remove one of the "X" overloads, I
properly get the error message. I went to dinkumware.com, and tried to
compile it online. The EDG front-end gives an error. Heck, even VC++ 7.1
gives an error. My Borland C++Builder 6.0 doesn't want to compile it
either. No compiler should. The overload somehow confuses VC++ 8.
Yep, t.X has no meaning in Standard C++, but earlier versions allowed it to
mean pointer to member. MS fixed this in VC7, but it looks like VC8 has
suffered a regression here.

--
Doug Harrison
Visual C++ MVP
Nov 9 '06 #4
Doug Harrison [MVP] wrote:
Yep, t.X has no meaning in Standard C++, but earlier versions allowed it to
mean pointer to member. MS fixed this in VC7, but it looks like VC8 has
suffered a regression here.
Only when there's an overload. If I remove either one of the X
functions, the compiler shows an error as expected, just like VC7. It
looks like an overloaded function declaration causes the compiler to
accept the object.Method expression without an error.

Tom
Nov 9 '06 #5

"Tamas Demjen" <td*****@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Doug Harrison [MVP] wrote:
>Yep, t.X has no meaning in Standard C++, but earlier versions allowed it
to
mean pointer to member. MS fixed this in VC7, but it looks like VC8 has
suffered a regression here.

Only when there's an overload. If I remove either one of the X functions,
the compiler shows an error as expected, just like VC7. It looks like an
overloaded function declaration causes the compiler to accept the
object.Method expression without an error.
That's perhaps not the problem so much as that the compiler finds a multiply
operator for it... there should at least be an error such as "Can't find any
operator* overload for parameters of [method group] and double".
>
Tom

Nov 10 '06 #6
On Fri, 10 Nov 2006 10:24:12 -0600, "Ben Voigt" <rb*@nospam.nospamwrote:
>That's perhaps not the problem so much as that the compiler finds a multiply
operator for it... there should at least be an error such as "Can't find any
operator* overload for parameters of [method group] and double".
The line in question was this:
>> z = t.X * y; // this will generate no error or warning but won't call t.X()
The sequence of tokens "t.X" has no meaning in Standard C++, so the
compiler should stop right there and report this as the error. I've
shortened the OP's example a bit, and it's interesting to see how VC8
handles a couple more permutations.

struct Test
{
void X(double val); // 1
double X(void);
};

int main()
{
Test t;
double z;

z = t.X; // 2
z = t.X * 2; // 3
}

***** (A) Line (3) commented out:

a.cpp(12) : error C2440: '=' : cannot convert from 'overloaded-function' to
'double'

***** (B) Line (2) commented out:

No error.

***** (C) Lines (1, 3) commented out:

a.cpp(12) : error C3867: 'Test::X': function call missing argument list;
use '&Test::X' to create a pointer to member
a.cpp(12) : error C2440: '=' : cannot convert from 'double (__thiscall
Test::* ) (void)' to 'double'

***** (D) Lines (1, 2) commented out:

a.cpp(13) : error C3867: 'Test::X': function call missing argument list;
use '&Test::X' to create a pointer to member
a.cpp(13) : error C2296: '*' : illegal, left operand has type 'double
(__thiscall Test::* )(void)'

All four of these should cause error C3867 and only that error. Cases (C)
and (D) indicate that even in the absence of overloading, the compiler is
letting its internal notion(?) of what "t.X" means slip through to the user
level, where it has no meaning whatsoever. IOW, error messages C2440 and
C2296 are completely bogus in this context. Note also the compiler didn't
report the real error for case (A).

--
Doug Harrison
Visual C++ MVP
Nov 10 '06 #7
Tamas Demjen wrote:
murphman wrote:

[...]
P.S. I haven't tried VC++ 2006 (Beta 1). It may be fixed there, I can't
verify it.
It seems to be fixed in VC++ 2006. It's a mess to program in a Virtual
PC remotely and I had to retype the sample char by char.
Tom
Andre
Nov 11 '06 #8
Andre Kaufmann wrote:
Tamas Demjen wrote:
>murphman wrote:

[...]
P.S. I haven't tried VC++ 2006 (Beta 1). It may be fixed there, I
can't verify it.

It seems to be fixed in VC++ 2006. It's a mess to program in a Virtual
PC remotely and I had to retype the sample char by char.
There's no such thing as VC++ 2006. Are you referring to VC 2005 SP1 or an
Orcas preview build?

-cd
Nov 11 '06 #9
murphman wrote:
I recently decided to do the right thing & write accessor functions
for my class member variables instead of having them public and
introduced a rather nasty bug. The class below shows the error -
basically if you forget to add the parenthesis in the call to
function X() then the program compiles ok with no warnings but gives
the wrong answer :(

I'm using VS2005 Prof edition v2.0.50727

The answer seems to be that if you are writing accessor functions
then don't use overloaded functions - maybe use setX & getX instead -
or make sure you don't forget the parenthesis! It seems to think I
want a function pointer(?) but I think it should at least give a
warning.
VC 2005 SP1 final reports this error:

bug111106.cpp(20) : error C3867: 'Test::X': function call missing argument
list;
use '&Test::X' to create a pointer to member

-cd
Nov 11 '06 #10
>VC 2005 SP1 final

It's finished? Where is it?

Dave
Nov 11 '06 #11
Carl Daniel [VC++ MVP] wrote:
Andre Kaufmann wrote:
>Tamas Demjen wrote:
>>murphman wrote:

[...]
P.S. I haven't tried VC++ 2006 (Beta 1). It may be fixed there, I
can't verify it.
It seems to be fixed in VC++ 2006. It's a mess to program in a Virtual
PC remotely and I had to retype the sample char by char.

There's no such thing as VC++ 2006. Are you referring to VC 2005 SP1 or an
Orcas preview build?
I referred to the same as my pre-poster ;-)
Yes - the Orcas preview build, which might either be named VC++ 2006/9.0
or rather VC++ 2007/9.0.
-cd
Andre
Nov 11 '06 #12
David Lowndes wrote:
>VC 2005 SP1 final

It's finished? Where is it?
It's not shipped yet, but I'm led to believe that the system I tried it on
is supposed to be the final SP1 bits.

-cd
Nov 11 '06 #13
Andre Kaufmann wrote:
>There's no such thing as VC++ 2006. Are you referring to VC 2005 SP1
or an Orcas preview build?


I referred to the same as my pre-poster ;-)
Sorry I was referring to VC++ 2005 SP 1.

Tom
Nov 13 '06 #14

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

Similar topics

6
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to...
8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
16
by: pj | last post by:
(Was originally, probably wrongly, posted to the vc subgroup.) (This doesn't appear to be a c# problem, but a problem with a bug in the Visual Studio c# compiler, but, any help will be welcome...)...
0
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
3
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
4
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The...
6
by: David Lack | last post by:
Hi, I recently installed a 60-day trial of .NET 2003 on my development system. I made tests with previous personal projects (which compiled ok with VC6) and some open source files, and keep...
19
by: David W | last post by:
float nanometers(long pm) { return pm / 1000.0f; } void f() { if(nanometers(309311L) == nanometers(309311L)) { // do something }
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.