470,863 Members | 1,139 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

error: invalid initialization of non-const reference of type

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.

Nov 22 '05 #1
9 23952
CQ wrote:
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
Nov 22 '05 #2
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.

Nov 22 '05 #3
CQ wrote:
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?
...


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.

--
Best regards,
Andrey Tarasevich
Nov 22 '05 #4
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!!
}

}

Nov 22 '05 #5
CQ wrote:
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?


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
Nov 22 '05 #6
CQ wrote:
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?


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

Nov 22 '05 #7

Victor Bazarov wrote:
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.


that's interesting. any references on this?

Nov 22 '05 #8

Aleksey Loginov wrote:
Victor Bazarov wrote:
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.


that's interesting. any references on this?


what a nice day :) found by myself
http://open-std.org/jtc1/sc22/wg21/d...2003/n1488.pdf

Nov 22 '05 #9
"CQ" <su*******@gmx.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.
Nov 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by DU | last post: by
6 posts views Thread by stef | last post: by
4 posts views Thread by Bret Pehrson | last post: by
8 posts views Thread by lovecreatesbea... | last post: by
7 posts views Thread by The|Godfather | last post: by
22 posts views Thread by Amali | last post: by
20 posts views Thread by JohnQ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.