473,653 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 24169
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.goo glegroups.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
4036
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 I am not fully convinced that this problem cannot be solved. I am going to propose a solution here. For the sake of discussion I will post my solution here. It is possible that the proposed solution does not work, feedback and comments are...
5
2741
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 render as best as they can elements (and/or attribute too? can't remember) that they ignore. e.g.: I know that fieldset will be rendered as a block level element in NS 4.7+, almost just like div.
6
385
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 `=' to initialize static data members) *** Error code 1 make: Fatal error: Command failed for target `driver.o' line 33 is :
4
2201
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
21145
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 completly know at link time, but will be symbols at the compile time. I used the following code for this initalization.. uint32 data_ptr_mask = ((uint32) data) & 0x3ff;
8
3110
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 initialized before the program starts executing." -- What does this mean? What is the name of the stage in which the mentioned initialization is performed? Compile-time or run-time? In the following snippet, variables b and c are defined at line 7...
7
14964
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 error/warning: "error: invalid use of nonstatic data member " However, he did NOT mention this error in the book explicitly.It happens always in the constructor when you try to initialize some data members in the constructor and try to accsess other data...
22
3253
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 seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
23
3649
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 the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
20
6083
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.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.