奇怪的编译器警告 [英] Strange compiler warning

查看:150
本文介绍了奇怪的编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Solaris Forte编译器5.4中有以下简单程序

产生警告。虽然它会产生警告,但它可以正常工作



这个编译正常,没有旧版5.1

编译器的任何警告。由于最新的编译器产生警告,它让我对我自己的代码感到怀疑。我仍然找不到它的任何问题

虽然。


每当一个

类的复制构造函数时,它本质上会产生一个警告使用纯虚函数调用传递给它的实例上的纯虚函数

方法。


例如: -

class X {

public:

virtual void foo()= 0;

X(){}

X(X& x){x.foo(); }

}


这是编译器错误。或者我错了。

感谢您的帮助

--sony


夸克:/ home / ffdfptz / t> CC -V

CC:Forte Developer 7 C ++ 5.4 2002/03/09

夸克:/ home / ffdfptz / t> CC aC

" a.C",第13行:警告:尝试调用纯虚函数

XXX :: foo()缺点

t将始终失败。

1检测到警告。

/////////////////////////// ////////节目


#include< iostream>

#include< string>


使用命名空间std;


class XXX {

int i_;

virtual int foo()const = 0;

public:

XXX(){}

XXX(const XXX& x):i_(x.foo()){cout<< hahha<< i_<< endl; }

};


类YY:public XXX {


int foo()const {return 13; }

public:

YY(){}

YY(const YY& y):XXX(y){}


};


int main(int argc,char * argv []){


YY y;

YY x(y);

返回0;

}

I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help
--sony

quark:/home/ffdfptz/t>CC -V
CC: Forte Developer 7 C++ 5.4 2002/03/09

quark:/home/ffdfptz/t>CC a.C
"a.C", line 13: Warning: Attempt to call a pure virtual function
XXX::foo() cons
t will always fail.
1 Warning(s) detected.
///////////////////////////////////Program

#include<iostream>
#include<string>

using namespace std ;

class XXX {
int i_ ;
virtual int foo() const = 0 ;
public:
XXX(){}
XXX( const XXX& x ) : i_( x.foo() ) {cout<<"hahha"<<i_<<endl ; }
};

class YY : public XXX {

int foo() const { return 13 ; }
public :
YY(){}
YY( const YY& y ) : XXX( y ) {}

};

int main( int argc, char* argv[] ){

YY y ;
YY x ( y ) ;
return 0 ;
}

推荐答案

" Sony Antony" <所以******** @ hotmail.com>写了...
"Sony Antony" <so********@hotmail.com> wrote...
我在Solaris Forte编译器5.4中有以下简单程序产生警告。虽然它会产生警告,但它可以正常工作

这在旧的5.1
编译器中没有任何警告就编译得很好。由于最新的编译器会产生警告,这让我对自己的代码感到怀疑。我仍然找不到任何问题
虽然。

每当具有纯虚函数的类的复制构造函数调用纯虚函数时,它本质上会产生警告。传递给它的实例上的方法。

例如: -
class X {
public:
virtual void foo()= 0;
X(){}
X(X& x){x.foo();这是一个编译器错误。或者我错了。
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.




如果从构造函数调用虚拟函数,则静态解析

或析构函数。从构造函数调用纯虚函数或

a析构函数会导致未定义的行为。


Victor



Virtual functions are resolved statically if called from constructors
or destructors. Calling a pure virtual function from a constructor or
a destructor causes undefined behaviour.

Victor


so********@hotmail.com (索尼安东尼)在留言中写道:< 3e**************************@posting.google。 com> ...
so********@hotmail.com (Sony Antony) wrote in message news:<3e**************************@posting.google. com>...
我在Solaris Forte编译器5.4中有以下简单程序产生警告。虽然它会产生警告,但它可以正常工作

这在旧的5.1
编译器中没有任何警告就编译得很好。由于最新的编译器会产生警告,这让我对自己的代码感到怀疑。我仍然找不到任何问题
虽然。

每当具有纯虚函数的类的复制构造函数调用纯虚函数时,它本质上会产生警告。传递给它的实例上的方法。

例如: -
class X {
public:
virtual void foo()= 0;
X(){}
X(X& x){x.foo();这是一个编译器错误。或者我错了。
感谢您的帮助
--sony

夸克:/ home / ffdfptz / t> CC -V
CC:Forte Developer 7 C ++ 5.4 2002/03/09
夸克:/ home / ffdfptz / t> CC aC
a.C,第13行:警告:尝试调用纯虚函数
XXX :: foo()缺点总是会失败。
1警告已经检测到。

//////////// ///////////////////////程序

#include< iostream>
#include< string>

使用命名空间std;

类XXX {
int i_;
virtual int foo()const = 0;
public:
XXX (){}
XXX(const XXX& x):i_(x.foo()){cout<<" hahha"<< i_<< endl; } YY:public XXX {

int foo()const {return 13;公开:
YY(){}
YY(const YY& y):XXX(y){}

};

int main(int argc,char * argv []){

YY y;
YY x(y);
返回0;
}
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help
--sony

quark:/home/ffdfptz/t>CC -V
CC: Forte Developer 7 C++ 5.4 2002/03/09

quark:/home/ffdfptz/t>CC a.C
"a.C", line 13: Warning: Attempt to call a pure virtual function
XXX::foo() cons
t will always fail.
1 Warning(s) detected.
///////////////////////////////////Program

#include<iostream>
#include<string>

using namespace std ;

class XXX {
int i_ ;
virtual int foo() const = 0 ;
public:
XXX(){}
XXX( const XXX& x ) : i_( x.foo() ) {cout<<"hahha"<<i_<<endl ; }
};

class YY : public XXX {

int foo() const { return 13 ; }
public :
YY(){}
YY( const YY& y ) : XXX( y ) {}

};

int main( int argc, char* argv[] ){

YY y ;
YY x ( y ) ;
return 0 ;
}




Comeau在线编译你的代码很好,没有任何警告。
http://www.comeaucomputing.com/tryitout/


这不是绝对的绝对保证,但是没有它太多了,不管怎么说,这是错误的。请记住,允许编译器警告

它喜欢什么。也许他们太难以保护你了。


GJD



Comeau online compiles your code fine, with no warnings.
http://www.comeaucomputing.com/tryitout/

That''s not an absolut guarantee of correctness, but there isn''t much
it gets wrong. Bear in mind that the compiler is allowed to warn about
anything it likes. Perhaps they are tyring too hard to protect you.

GJD


因为我从未使用过该编译器而只是猜测。 />

当这个构造函数运行时,子类还没有被构造成

,所以你不应该调用你孩子的成员函数,比如

执行是我认为未定义的行为。也许你的

编译器的未来版本可能不允许这种情况发生。你期待从哪里跑出来?



Tom


" Sony Antony" <所以******** @ hotmail.com>在消息中写道

news:3e ************************** @ posting.google.c om ...
Just guessing since I have never used that compiler.

When this constructor is running child classes have not yet been
constructed, so you should not call your childrens member functions, such
execution is I think undefined behaviour. Perhaps future versions of your
compiler may not allow this to happen. Which foo are you expecting to run
from here?

Tom

"Sony Antony" <so********@hotmail.com> wrote in message
news:3e**************************@posting.google.c om...
我在Solaris Forte编译器5.4中有以下简单程序产生警告。虽然它会产生警告,但它可以正常工作

这在旧的5.1
编译器中没有任何警告就编译得很好。由于最新的编译器会产生警告,这让我对自己的代码感到怀疑。我仍然找不到任何问题
虽然。

每当具有纯虚函数的类的复制构造函数调用纯虚函数时,它本质上会产生警告。传递给它的实例上的方法。

例如: -
class X {
public:
virtual void foo()= 0;
X(){}
X(X& x){x.foo();这是一个编译器错误。或者我错了。
感谢您的帮助
--sony

夸克:/ home / ffdfptz / t> CC -V
CC:Forte Developer 7 C ++ 5.4 2002/03/09
夸克:/ home / ffdfptz / t> CC aC
a.C,第13行:警告:尝试调用纯虚函数
XXX :: foo()缺点总是会失败。
1警告已经检测到。

//////////// ///////////////////////程序

#include< iostream>
#include< string>

使用命名空间std;

类XXX {
int i_;
virtual int foo()const = 0;
public:
XXX (){}
XXX(const XXX& x):i_(x.foo()){cout<<" hahha"<< i_<< endl; } YY:public XXX {

int foo()const {return 13;公开:
YY(){}
YY(const YY& y):XXX(y){}

};

int main(int argc,char * argv []){

YY y;
YY x(y);
返回0;
}
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help
--sony

quark:/home/ffdfptz/t>CC -V
CC: Forte Developer 7 C++ 5.4 2002/03/09

quark:/home/ffdfptz/t>CC a.C
"a.C", line 13: Warning: Attempt to call a pure virtual function
XXX::foo() cons
t will always fail.
1 Warning(s) detected.
///////////////////////////////////Program

#include<iostream>
#include<string>

using namespace std ;

class XXX {
int i_ ;
virtual int foo() const = 0 ;
public:
XXX(){}
XXX( const XXX& x ) : i_( x.foo() ) {cout<<"hahha"<<i_<<endl ; }
};

class YY : public XXX {

int foo() const { return 13 ; }
public :
YY(){}
YY( const YY& y ) : XXX( y ) {}

};

int main( int argc, char* argv[] ){

YY y ;
YY x ( y ) ;
return 0 ;
}



这篇关于奇怪的编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆