error: invalid initialization of non-const reference of type

C

CQ

Hi everyone,

I get the following error when compiling my code:

error: invalid initialization of non-const reference of type 'Vertex&'
from a temporary of type 'int'

The implementation of the function is as follows:

Vertex& IGEdge::Mate (Vertex const& vertex) {
if (v0 == vertex) {
return v1;
} else {
return 0;
}
}

What do you suggest to do? And why can't I return a simple
NULL-Pointer? All I want to do is to return a NULL in case my vertex v0
does not match the vertex being passed as a parameter.


Thanks a lot in advance,

CQ.
 
V

Victor Bazarov

CQ said:
I get the following error when compiling my code:

error: invalid initialization of non-const reference of type 'Vertex&'
from a temporary of type 'int'

The implementation of the function is as follows:

Let's simplify:
Vertex& IGEdge::Mate (Vertex const& vertex) {
return 0;

What would that accomplish? There are no "null references" in C++.
}

What do you suggest to do? And why can't I return a simple
NULL-Pointer?

Who says you can't? Please, by all means, do return a null *pointer*.
What you're trying to return is a "null reference" which doesn't exist.
> All I want to do is to return a NULL in case my vertex v0
does not match the vertex being passed as a parameter.

Fine. But in that case your return type has to be a _pointer_, not
a _reference_.

V
 
C

CQ

Who says you can't? Please, by all means, do return a null *pointer*.
What you're trying to return is a "null reference" which doesn't exist.

But how do I create a null *pointer* that I can return? Is there a
construct in C++ that I can use?

Thanx,

CQ.
 
V

Victor Bazarov

CQ said:
But how do I create a null *pointer* that I can return? Is there a
construct in C++ that I can use?

In the future versions there will be a separate keyword, but for now 0
should suffice. Make sure that the return value type is a pointer.

V
 
A

Art Stamness

Either change the call to actually reaturn a *POINTER*, which you
currently aren't doing, you are returning a *REFERENCE*. The other
error being, you are trying to uprade ( or downgrade, I forget what
they call it ) your type to be non-const. If you pass in a const
reference, you can't return a pointer to it that is not const. ( Well
you can, with a little extra magic, but you shouldn't )

Ex. 1
// This will do what you want, if you wat a pointer
const Vertex* IGEdge::Mate (Vertex const& vertex) {
if (v0 == vertex) {
return & v1;
} else {
return 0;
}

}

Ex. 2
// This is what returning a null reference looks like, see if you can
find whats wrong
Vertex& IGEdge::Mate (Vertex const& vertex) {
if (v0 == vertex) {
return v1;
} else {
return * (Vertex * ) 0; // I hope you see whats wrong
with this!!
}

}
 
A

Andrey Tarasevich

CQ said:
But how do I create a null *pointer* that I can return? Is there a
construct in C++ that I can use?
...

You have to decide what is it you want to return. Your code is written
as if you want to return a _reference_. Now you are talking about
returning a _pointer_. So, what is it? Make up your mind. Pointers and
references are different things.
 
R

Rolf Magnus

CQ said:
But how do I create a null *pointer* that I can return? Is there a
construct in C++ that I can use?

Yes. Make the return type a Vertex* instead of a Vertex&, i.e. a *pointer*
instead of a *reference*.
 
A

Aleksey Loginov

Victor said:
In the future versions there will be a separate keyword, but for now 0
should suffice. Make sure that the return value type is a pointer.

that's interesting. any references on this?
 
J

Jim Langston

CQ said:
Hi everyone,

I get the following error when compiling my code:

error: invalid initialization of non-const reference of type 'Vertex&'
from a temporary of type 'int'

The implementation of the function is as follows:

Vertex& IGEdge::Mate (Vertex const& vertex) {
if (v0 == vertex) {
return v1;
} else {
return 0;
}
}

What do you suggest to do? And why can't I return a simple
NULL-Pointer? All I want to do is to return a NULL in case my vertex v0
does not match the vertex being passed as a parameter.

First suggestion, change it to return a pointer instead of a reference.

If you absolutely must return a reference (don't know why) perhaps return a
structure.

struct IGEdgeReturn
{
bool Good;
Vertex& Value;
}

and return that, seting Value to whatever, just ignore it in code if it's
not .good

I'm not saying you should do this, just saying you can.
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top