strange compiler error ( gcc 4.0.2)

G

grundmann

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
 
R

Ron Natalie

grundmann said:
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.
 
J

John Harrison

grundmann said:
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
 
G

grundmann

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
 
B

Ben Pope

grundmann said:
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
 
G

grundmann

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
 
J

John Harrison

grundmann said:
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
 
J

John Harrison

John said:
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
 
G

grundmann

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top