编译问题 [英] Compiler Problem
问题描述
我最近决定做正确的事情&为我的
类成员变量编写访问器函数,而不是将它们公开,并引入了一个相当不错的b $ b恶意错误。下面的类显示错误 - 基本上如果你忘记在函数X()的调用中添加
,那么程序编译好了
没有警告但给出了错误答案:(
我正在使用VS2005 Prof版本v2.0.50727
答案似乎是如果你正在编写访问器函数然后不要
使用重载函数 - 也许使用setX& getX - 或者确保你
不要忘记括号!它似乎在想我想要一个函数指针(?)
但我认为它至少应该发出警告。
class Test {
public:
Test():m_Num(10){}
void X(const double val){m_Num = val;}
double X(无效){return m_Num;};
私人:
double m_Num;
};
int _tmain(int argc,_TCHAR * argv [])
{
测试t;
double y = 5.0;
doublel e z;
// z = t.X //这将产生错误
z = t.X * y; //这不会产生错误或警告,但不会打电话给tX()
返回0;
}
我已经google了这个,但我很惊讶我没有看到更多这个问题
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
推荐答案
murphman写道:
murphman wrote:
class Test {
public:
Test():m_Num(10){}
void X(const double val){m_Num = val;}
double X(void){return m_Num;};
私人:
double m_Num;
};
int _tmain(int argc,_TCHAR * argv [ ])
{
测试t;
双y = 5.0;
双z;
// z = tX //这会产生错误
z = tX * y; //这不会产生任何错误或警告,但不会调用tX()
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()
这是一个编译错误,这段代码不应该编译,它'肯定是
错误,甚至没有警告。如果我删除其中一个X,重载,我
正确获取错误消息。我去了dinkumware.com,并尝试在线编译
。 EDG前端出错。哎呀,即使是VC ++ 7.1
也会出错。我的Borland C ++ Builder 6.0不想编译它
。没有编译器应该。过载会让VC ++ 8感到困惑。
你能在MS上提交错误报告吗?
P.S.我还没有尝试过VC ++ 2006(Beta 1)。它可以在那里固定,我不能
验证它。
Tom
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
谢谢 - 我会提交错误报告
干杯
Joe
" Tamas Demjen"写道:
Thanks - I''ll file a bug report
Cheers
Joe
"Tamas Demjen" wrote:
murphman写道:
murphman wrote:
class Test {
public:
Test():m_Num(10){}
void X(const double val){m_Num = val;}
>
double X(无效){return m_Num;};
私人:
double m_Num;
} ;
int _tmain(int argc,_TCHAR * argv [])
{
测试t;
double y = 5.0;
double z;
// z = tX //这会产生错误
z = tX * y; //这不会产生任何错误或警告,但不会调用tX()
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()
这是一个编译错误,这段代码不应该编译,它'肯定是
错误,甚至没有警告。如果我删除其中一个X,重载,我
正确获取错误消息。我去了dinkumware.com,并尝试在线编译
。 EDG前端出错。哎呀,即使是VC ++ 7.1
也会出错。我的Borland C ++ Builder 6.0不想编译它
。没有编译器应该。过载会让VC ++ 8感到困惑。
你能在MS上提交错误报告吗?
P.S.我还没有尝试过VC ++ 2006(Beta 1)。它可以固定在那里,我不能
验证它。
Tom
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
>
2006年11月9日星期四09:19:33 -0800,Tamas Demjen< td ***** @ yahoo.comwrote:
On Thu, 09 Nov 2006 09:19:33 -0800, Tamas Demjen <td*****@yahoo.comwrote:
> ; murphman写道:
>murphman wrote:
> class Test {
public:
Test():m_Num(10){}
void X(const double val){m_Num = val;}
double X(void){return m_Num;};
私人:
double m_Num;
};
int _tmain(int argc,_TCHAR * argv [])
{
测试t;
double y = 5.0;
double z;
// z = tX //这将产生错误
z = tX * y; //这不会产生任何错误或警告,但不会调用tX()
>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()
这是一个编译器错误,这段代码不应该编译,它肯定是一个
错误,甚至没有警告。如果我删除其中一个X,重载,我正确地得到错误信息。我去了dinkumware.com,并尝试在线编译它。 EDG前端出错。哎呀,即使是VC ++ 7.1
也会出错。我的Borland C ++ Builder 6.0也不想编译它。没有编译器应该。重载会以某种方式混淆VC ++ 8.
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.
是的,tX在标准C ++中没有意义,但早期版本允许它
表示指向会员。 MS在VC7中修复此问题,但看起来VC8已经在这里遭遇了回归。
-
Doug Harrison
Visual C ++ MVP
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
这篇关于编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!