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

error: invalid initialization of non-const reference of type

P: n/a
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
Share this Question
Share on Google+
9 Replies


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

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

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

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

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

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

P: n/a

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

P: n/a

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

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