Does not compile

P

parag_paul

#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
 
R

red floyd

#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.
 
A

Andrey Tarasevich

#include <iostream>
using namespace std;

void ha(int& j);

void ha(int& j) {
cout <<j<<endl;
}


main()

int 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

'NULL' is a substitute for and integral constant expression of value 0.
It is not an lvalue. In C++ a non-const reference (parameter of 'ha')
can only be bound to an lvalue. 'NULL' is not one, which is why you
can't call your 'ha' function with 'NULL' argument.

Either specify an lvalue as an argument

int j = 0;
ha(j); // OK

or make your 'ha' accept a const reference

void ha(const int& j) {
cout <<j<<endl;
}
...
ha(NULL); // OK

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.
 
P

parag_paul

int main()



'NULL' is a substitute for and integral constant expression of value 0.
It is not an lvalue. In C++ a non-const reference (parameter of 'ha')
can only be bound to an lvalue. 'NULL' is not one, which is why you
can't call your 'ha' function with 'NULL' argument.

Either specify an lvalue as an argument

int j = 0;
ha(j); // OK

or make your 'ha' accept a const reference

void ha(const int& j) {
cout <<j<<endl;
}
...
ha(NULL); // OK

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?
 
I

Ian Collins

*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.
 
S

Shadowman

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)
 
V

Victor Bazarov

Shadowman said:
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".

V
 
S

Shadowman

Victor said:
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...
 
J

James Kanze

int main()
'null' is a substitute for and integral constant expression of
value 0. it is not an lvalue.

it can be, but then it must have a const type, and cannot bind
to a non-const reference either.

[...]
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.

Good compilers warn if NULL is used in a non-pointer context,
e.g.:

$ cat null.cc
#include <cstddef>

int
main()
{
int i = NULL ;
return 0 ;
}
$ g++ null.cc
null.cc: In function 'int main()':
null.cc:6: warning: converting to non-pointer type 'int' from NULL
 
J

James Kanze

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>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top