473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 24152
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
5
by: DU | last post by:
Hi! I really need to understand what is so-called browser error correction mechanisms. Can you explain this? I read somewhere (and I no longer can find where I read that) that browsers try to...
6
by: stef | last post by:
Anyone have any guesses as to why I am getting an error message when I try to compile: In file included from CP.h:13,from driver.cpp:5: cb.h:33: invalid data member initialization cb.h:33: (use...
4
by: Bret Pehrson | last post by:
I just stumbled across the following problem: //.h class Masses { static double mass1; static double mass2; static double mass3; };
10
by: PB | last post by:
Hi ! I have the following code, which I am using in an Embedded systems, c-compiler.. However I see the same problem with GCC too.. I need the last 10 bits of an address pointer, which is...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.