473,387 Members | 1,517 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,387 software developers and data experts.

Does not compile

#include <iostream>
using namespace std;

void ha(int& j);

void ha(int& j) {
cout <<j<<endl;
}
main()
{
int j = 0;
ha(NULL);
}
Why so ,
gcc version 3.3.6
p236.cc: In function `int main()':
p236.cc:14: error: invalid initialization of non-const reference of
type 'int&'
from a temporary of type 'long int'
p236.cc:6: error: in passing argument 1 of `void ha(int

Nov 2 '07 #1
5 1366
pa********@hotmail.com wrote:
#include <iostream>
using namespace std;

void ha(int& j);

void ha(int& j) {
cout <<j<<endl;
}
main()
{
int j = 0;
ha(NULL);
}
Why so ,
gcc version 3.3.6
p236.cc: In function `int main()':
p236.cc:14: error: invalid initialization of non-const reference of
type 'int&'
from a temporary of type 'long int'
p236.cc:6: error: in passing argument 1 of `void ha(int
Because NULL aka 0 is not an lvalue, it's an rvalue. There is no
variable to "refer" to. If ha() too a const int& as a param, then you
could bind it to the temporary (created by the call).

Or are you working on the assumption that a reference is a pointer, and
you're trying to create a NULL reference?

In that case, please repeat after me: A REFERENCE IS NOT A POINTER.
It may be (and probably *is*) implemented as such, but an implementation
is not required to do so.

Nov 2 '07 #2
pa********@hotmail.com wrote:
On Nov 2, 12:32 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
>It is worth noting though, that NULL macro should normally be used in
pointer context and using it in place of an 'int' value is not a good
practice.
*Please* don't quote signatures.
>
That helps thanks a lot.
I did not get the NULL macro.
Where is it defined?
It can be defined in a number of places (probably for historical
reasons), the standard lists them in C 2.2.3. For your implementation,
you'll have to do a search.

--
Ian Collins.
Nov 2 '07 #3
pa********@hotmail.com wrote:
[..]
I did not get the NULL macro.
Where is it defined?
<cstdlib>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 2 '07 #4
Victor Bazarov wrote:
Shadowman wrote:
>pa********@hotmail.com wrote:
>>That helps thanks a lot.
I did not get the NULL macro.
Where is it defined?
I've found it in several header files as:

#define NULL 0

or

#define NULL ((void*)0)

The latter is not legal in C++ because it is not an "integral constant
expression of value 0".
OK, sorry, I must have been looking in C library headers...

--
SM
rot13 for email
Nov 2 '07 #5
On Nov 2, 9:39 pm, Ian Collins <ian-n...@hotmail.comwrote:
parag_p...@hotmail.com wrote:
On Nov 2, 12:32 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
It is worth noting though, that NULL macro should normally be used in
pointer context and using it in place of an 'int' value is not a good
practice.
That helps thanks a lot.
I did not get the NULL macro.
Where is it defined?
It can be defined in a number of places (probably for
historical reasons), the standard lists them in C 2.2.3. For
your implementation, you'll have to do a search.
It must be defined in all of those places, according to the
standard. Or more correctly, it must be defined after you have
included any of those headers.

The usual header, if you just want NULL, is <cstddef>.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
4
by: Alex Vinokur | last post by:
Compiler GNU gpp.exe (GCC) 3.4.1 Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that? ------ foo.cpp ------ struct Foo { explicit Foo(int) {}
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
11
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
5
by: Jeff | last post by:
hi asp.net 2.0 I get this compile error: 'Image' does not contain a definition for 'ImageUrl' Image image = (Image)e.Item.FindControl("img"); image.ImageUrl = "~/image.png";
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.