2012-01-17 28 views
4

我有一个对象是合法的在初始化器列表使用返回临时功能

Segment::Segment(QPointF const& start, QPointF const& end): 
    mOrigin(toVector3df(start)),mEnd(toVector3df(end)){  
} 

mOrigin的下面的构造是Vector3df型和toVector3df(QPointF const&)返回临时Vector3df功能。到现在为止还挺好。代码编译得很好,并且像linux下的魅力一样工作,gcc 4.4.3。大多数警告被激活。

现在我想交叉编译的诺基亚智能手机(Meamo弗里曼特尔) 和突然的相同的代码,我得到很奇怪的编译器警告:

include/vector3d.h: In constructor 'Segment::Segment(const QPointF&, const QPointF&)': 
include/vector3d.h:64: warning: 'this.902' is used uninitialized in this function 
include/vector3d.h:64: note: 'this.902' was declared here 

首先:当然没有真正的'Vecto3df'内部的变量名为this.902,所以我的第一个问题是:“有人看到过这样的警告吗?”另外Vector3df构造函数没有错,它们非常简单,toVector3df(QPointF const&)是一个非线性的非成员模板函数,可以在代码的其他部分工作。 Vector3df继承自只定义非成员函数的模板,没有变量no,虚函数。

其次,当我在上面的代码更改为以下

Segment::Segment(QPointF const& start, QPointF const& end): 
    mOrigin(),mEnd(){ 
    mOrigin = toVector3df(start); 
    mEnd = toVector3df(end); 
} 

的代码工作正常,没有任何警告。 那么我在这里错过了什么?有没有人知道警告的起源可能是什么。我违反了一些我不知道的原则吗? fremantle编译器(Maemo 5,Qt 4.6.2)更严重还是越野车?

在此先感谢,马丁

编辑: 这里是一个小例子,比较遗憾的是长度:-P

#include <iostream> 
#include <sstream> 
#include <QPoint> 

template<typename T> class IoEnabled {}; 

template<typename T> 
class Vector3d: public IoEnabled<Vector3d<T> > { 
    private: 
    T mX; T mY; T mZ; 
    public: 
    Vector3d(T const& x, T const& y, T const& z=0.0) : mX(x), mY(y), mZ(z) {} 
}; 
typedef Vector3d<float> Vector3df; 

template<class T> 
Vector3df toVector3df(T const& p){ 
    return Vector3df(p.x(),p.y(),0.0); 
} 

class Segment { 
    private: 
    Vector3df mOrigin; Vector3df mEnd; 
    public: 
    Segment(QPointF const& start, QPointF const& end): 
     mOrigin(toVector3df(start)),mEnd(toVector3df(end)){ 
     //if toVector3df(...) is moved from the initializer to the body it works 
    } 
}; 

int main(int argc, char **argv) { 
    (void) argc; (void) argv; 
    Segment temp(QPointF(1,2),QPointF(3,4)); 
    return 0; 
} 

编译器调用:

g++ -c -pipe -Werror -Wall -Wextra -Wunused -Wundef -Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -O3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -D_REENTRANT -Wall -W -DQT_GL_NO_SCISSOR_TEST -DQT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH=1024 -DMAEMO -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/share/qt4/mkspecs/linux-g++-maemo5 -I. -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include/QtCore -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include/QtGui -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include -Isrc -Irelease/moc -o release/obj/main.o src/main.cpp 

模板似乎继承至关重要的是,如果Vector3d没有继承一切正常。

+0

'toVector3df'是否是成员函数? – 2012-01-17 17:19:04

+0

没有非会员模板 – Martin 2012-01-17 17:22:44

+0

你可以在一个小例子中重现这个问题,你可以粘贴到问题中吗? – 2012-01-17 17:34:23

回答

3

使用返回成员初始值设定项列表中的临时函数没有任何问题。
即使成员将被撤销的顺序在标准中也有很好的定义。

+0

好的,这也是我的印象,看到它确认后松了一口气。想知道编译器在抱怨什么? – Martin 2012-01-17 17:20:45

+0

@Martin:你可以创建一个小的简约独立代码,并在编译器中编译问题以重现错误? – 2012-01-17 17:23:01

+0

最后是的,看我的编辑。如果你想尝试它,请注意编译器参数。有点不同(没有O3)不会导致错误 – Martin 2012-01-17 18:31:05