strange compiler error ( gcc 4.0.2)

Discussion in 'C++' started by grundmann, Sep 24, 2005.

  1. grundmann

    grundmann Guest

    Hello,

    i got a strange compiler error.
    When compiling the following:

    // forward declarations
    typedef AvlTree<LineSegment,LineSegmentComperator> LSTree;
    void handleEventPoint (const EventPoint& , LSTree& , double&,
    std::list<IntersectionPoint>& );


    // the interesting function
    std::list<IntersectionPoint> findIntersections ( const
    std::list<LineSegment*>& segments)
    {
    // need two data structers
    AvlTree<EventPoint> eventQueue;
    double offset;
    LineSegmentComperator lsc(&offset);
    LSTree sweepStatus ( lsc );

    //output
    std::list<IntersectionPoint> output;

    // ripped some lines here

    while ( ! eventQueue.isEmpty () )
    {
    EventPoint p = eventQueue.min();
    eventQueue.deleteElem(p);
    // pass sweepStatus by reference
    50: handleEventPoint (p, sweepStatus, offset, output );
    }

    return output;
    }

    everything works fine.
    But if I replace the following lines:

    LineSegmentComperator lsc(&offset);
    LSTree sweepStatus ( lsc );

    with this shorter version

    LSTree sweepStatus ( LineSegmentComperator(&offset) );

    I always got this strange error from gcc:
    geometricalgos.cpp: In function 'std::list<IntersectionPoint,
    std::allocator<IntersectionPoint> > findIntersections(const
    std::list<LineSegment*, std::allocator<LineSegment*> >&)':
    geometricalgos.cpp:50: error: invalid initialization of non-const
    reference of type 'LSTree&' from a temporary of type 'LSTree
    (*)(LineSegmentComperator&)'
    geometricalgos.cpp:9: error: in passing argument 2 of 'void
    handleEventPoint(const EventPoint&, LSTree&, double&,
    std::list<IntersectionPoint, std::allocator<IntersectionPoint> >&)'

    I don't understand this. My Object sweepStatus is not temporary, there
    is no type-conversion, nor any creation of a temporary instance of
    LSTree. The only thing changed is, that I pass now a temporary object
    for constructor of LSTree. But the constructor is defined as follows:

    template <class T, class Compare>
    AvlTree<T,Compare>::AvlTree (const Compare& comp ) : root_ (0),
    comp_(comp)
    {

    };

    so the passed Compare-Object is copied to the internal member variable
    comp_, which is of type Comp.

    Does anybody know, where the problem is?

    Best Regards,

    Matthias Grundmann
    grundmann, Sep 24, 2005
    #1
    1. Advertising

  2. grundmann

    Ron Natalie Guest

    grundmann wrote:

    > LSTree sweepStatus ( lsc );


    What is LineSegmentComparater defined to be?

    Somehow it would appear that sweepStatus is being
    interpretted as a function declaration. This is
    possible if lsc somehow itself was a type name.
    Ron Natalie, Sep 24, 2005
    #2
    1. Advertising

  3. grundmann wrote:
    > Hello,
    >
    > i got a strange compiler error.
    > When compiling the following:
    >
    > // forward declarations
    > typedef AvlTree<LineSegment,LineSegmentComperator> LSTree;
    > void handleEventPoint (const EventPoint& , LSTree& , double&,
    > std::list<IntersectionPoint>& );
    >
    >
    > // the interesting function
    > std::list<IntersectionPoint> findIntersections ( const
    > std::list<LineSegment*>& segments)
    > {
    > // need two data structers
    > AvlTree<EventPoint> eventQueue;
    > double offset;
    > LineSegmentComperator lsc(&offset);
    > LSTree sweepStatus ( lsc );
    >
    > //output
    > std::list<IntersectionPoint> output;
    >
    > // ripped some lines here
    >
    > while ( ! eventQueue.isEmpty () )
    > {
    > EventPoint p = eventQueue.min();
    > eventQueue.deleteElem(p);
    > // pass sweepStatus by reference
    > 50: handleEventPoint (p, sweepStatus, offset, output );
    > }
    >
    > return output;
    > }
    >
    > everything works fine.
    > But if I replace the following lines:
    >
    > LineSegmentComperator lsc(&offset);
    > LSTree sweepStatus ( lsc );
    >
    > with this shorter version
    >
    > LSTree sweepStatus ( LineSegmentComperator(&offset) );
    >
    > I always got this strange error from gcc:
    > geometricalgos.cpp: In function 'std::list<IntersectionPoint,
    > std::allocator<IntersectionPoint> > findIntersections(const
    > std::list<LineSegment*, std::allocator<LineSegment*> >&)':
    > geometricalgos.cpp:50: error: invalid initialization of non-const
    > reference of type 'LSTree&' from a temporary of type 'LSTree
    > (*)(LineSegmentComperator&)'
    > geometricalgos.cpp:9: error: in passing argument 2 of 'void
    > handleEventPoint(const EventPoint&, LSTree&, double&,
    > std::list<IntersectionPoint, std::allocator<IntersectionPoint> >&)'
    >
    > I don't understand this. My Object sweepStatus is not temporary, there
    > is no type-conversion, nor any creation of a temporary instance of
    > LSTree. The only thing changed is, that I pass now a temporary object
    > for constructor of LSTree. But the constructor is defined as follows:


    Compiler (correctly) thinks that

    LSTree sweepStatus ( LineSegmentComperator(&offset) );

    is a function prototype.

    Try this

    LSTree sweepStatus = LSTree(LineSegmentComperator(&offset));

    john
    John Harrison, Sep 24, 2005
    #3
  4. grundmann

    grundmann Guest

    Why should the compiler think, that
    LSTree sweepStatus ( LineSegmentComperator(&offset) );
    is a function prototype. Isn't it a normal declaration of a object with
    calling the constructor passing the needed parameters?

    I also tried your syntax before, confusing about that gcc claimed that
    the copy-constructor is private. I have done this in the class AvlTree,
    because copying should not be allowed. But the "Type obj = Type (
    ....);" syntax should not invoke the copy-constructor. The compiler
    should recognise, that the rhs Object is temporary and should call the
    constructor instead. I am pretty sure, that MS VC-Compiler is working
    this way, but I am not sure about gcc.

    @Ron:
    The class LineSegmentComperator is implemented as follows:

    class LineSegmentComperator
    {
    public:
    explicit LineSegmentComperator ( double* offset =0) : offset_ (offset)
    {}
    bool operator()(const LineSegment& lhs, const LineSegment& rhs);
    protected:
    double* offset_;
    };

    I am not yet understanding the error.

    Best regards,

    Matthias Grundmann

    John Harrison schrieb:

    > Compiler (correctly) thinks that
    >
    > LSTree sweepStatus ( LineSegmentComperator(&offset) );
    >
    > is a function prototype.
    >
    > Try this
    >
    > LSTree sweepStatus = LSTree(LineSegmentComperator(&offset));
    >
    > john
    grundmann, Sep 24, 2005
    #4
  5. grundmann

    Ben Pope Guest

    grundmann wrote:
    > Why should the compiler think, that
    > LSTree sweepStatus ( LineSegmentComperator(&offset) );
    > is a function prototype. Isn't it a normal declaration of a object with
    > calling the constructor passing the needed parameters?
    >
    > I also tried your syntax before, confusing about that gcc claimed that
    > the copy-constructor is private. I have done this in the class AvlTree,
    > because copying should not be allowed. But the "Type obj = Type (
    > ....);" syntax should not invoke the copy-constructor. The compiler
    > should recognise, that the rhs Object is temporary and should call the
    > constructor instead. I am pretty sure, that MS VC-Compiler is working
    > this way, but I am not sure about gcc.


    The compiler is allowed to optimise away the copy, but the fact remains that the syntax:

    A a = A(...);

    Is akin to doing:
    A a(A(...));

    And so, the compiler must act as if the copy constructor is required, but it is private.

    Ben
    --
    I'm not just a number. To many, I'm known as a String...
    Ben Pope, Sep 24, 2005
    #5
  6. grundmann

    grundmann Guest

    I tried this and you are right, allowing the Type AvlTree to be
    copy-constructable make the compiler able to accept without any errors.
    But the initial expression

    LSTree sweepStatus (LineSegmentComperator (&offset));

    further leads to the error, stated above. Why?

    Matthias Grundmann
    grundmann, Sep 24, 2005
    #6
  7. grundmann wrote:
    > I tried this and you are right, allowing the Type AvlTree to be
    > copy-constructable make the compiler able to accept without any errors.
    > But the initial expression
    >
    > LSTree sweepStatus (LineSegmentComperator (&offset));
    >
    > further leads to the error, stated above. Why?
    >
    > Matthias Grundmann
    >


    Because, as I stated, the above is a function prototype. Nothing to do
    with copy constructors.

    john
    John Harrison, Sep 24, 2005
    #7
  8. John Harrison wrote:
    > grundmann wrote:
    >
    >> I tried this and you are right, allowing the Type AvlTree to be
    >> copy-constructable make the compiler able to accept without any errors.
    >> But the initial expression
    >>
    >> LSTree sweepStatus (LineSegmentComperator (&offset));
    >>
    >> further leads to the error, stated above. Why?
    >>
    >> Matthias Grundmann
    >>

    >
    > Because, as I stated, the above is a function prototype. Nothing to do
    > with copy constructors.
    >


    Presumably you have no trouble accepting

    LSTree sweepStatus(LineSegmentComperator &offset);

    as a function prototype. Presumably you also think that the extra pair
    of parens makes a difference but it does not.

    john
    John Harrison, Sep 24, 2005
    #8
  9. grundmann

    grundmann Guest

    Thank you for your help. I tried this once again with intel c++ 9.0 and
    got the same error. I have always thought that LineSegmentComperator
    (&offset) would be interpreted in any situations as an constructor call
    returning an object and not as an declaration of a reference of type
    LineSegmentComperator. So I was wrong.
    grundmann, Sep 24, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,133
  2. Ram
    Replies:
    0
    Views:
    2,834
  3. Gernot Frisch

    gcc strange error

    Gernot Frisch, Aug 6, 2004, in forum: C++
    Replies:
    2
    Views:
    413
    John Harrison
    Aug 6, 2004
  4. Tsb

    GCC compiler error

    Tsb, Oct 12, 2007, in forum: C Programming
    Replies:
    46
    Views:
    1,393
    Spoon
    Oct 22, 2007
  5. J Cook

    Strange gcc compiler error?

    J Cook, May 3, 2011, in forum: C++
    Replies:
    6
    Views:
    270
    James Kanze
    May 3, 2011
Loading...

Share This Page