By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,125 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,125 IT Pros & Developers. It's quick & easy.

strange compiler error ( gcc 4.0.2)

P: n/a
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

Sep 24 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
Sep 24 '05 #2

P: n/a
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
Sep 24 '05 #3

P: n/a
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


Sep 24 '05 #4

P: n/a
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...
Sep 24 '05 #5

P: n/a
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

Sep 24 '05 #6

P: n/a
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
Sep 24 '05 #7

P: n/a
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
Sep 24 '05 #8

P: n/a
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.

Sep 24 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.