The name pretty much says it all. How do you do it, specifically in header files?
Printable View
The name pretty much says it all. How do you do it, specifically in header files?
use = after the parameter with the default value, this way if the parameter is missing when called this default value will be used, example:prints 3. hope this helpsCode:#include<iostream>
int stuff(int a=3);
int main()
{
std::cout<< stuff();
}
int stuff(int a)
{
return a;
}
Well, the problem that I'm having is in my header file... If I declare a function that takes an optional argument of NULL, it says NULL was not declared in this scope. What exactly does that mean?
I believe you want null not NULL
EDIT: Just tested it, hmm odd.
do you have the proper code to reference the defined NULL value? try to include<iostream>. or instead of NULL just use 0 (zero) if need be. or post the relevant code if you can
NULL is defined in windef.h (which you get if you include windows.h) I agree though, 0 is what the MinGW implementation of Win32 API defines it as...
I don't have Win SDK from MS installed on this computer so I can't check what MS defines it as, but I will assume it is simular.Code:#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void*)0)
#endif
#endif
i get error if i try and reference NULL, however when iostream is included it works..since this is a popular header its not a bad idea to include this if it solves the problem.
windef??? NULL is defined in cstddef and cstdlib. That it works when including iostream is just coincidence.
Or just use the number 0. It works the same in C++.
Ahh, well it may be defined there too, I just know it was defined in the windows headers. I used Code::Blocks find declaration feature (I <3 it)
well it looks like theres afew CORRECT solutions here. he needs to include the most appropriate header which has a reference to the NULL definition.
Thanks! iostream, huh? A little odd.
The most appropriate if you need NULL is <cstddef>. <iostream> is inappropriate.
why is inappropriate? it worked in my case, and ive read on university notes that it is defined in iostream.
im not trying to start an argue im trying to see why you say its better than what i have said. thanks
The easiest solution is to #include <cstddef> in the header file.Quote:
Originally Posted by nadroj
thats not easier then including <iostream>.. im asking why is 'cstddef' 'better'
Using <cstddef> is better because that is where the standard says that NULL is defined. If you use iostream, it may work for you, but it might not work when you switch compilers, it might not work if you give your code to somebody else, and it might not work if you post code here for somebody to help you with and they try to compile it on their compiler.
Of course, it really doesn't matter because unless you are writing code that needs to work in C also, you should just use 0.
Probably because it is not standard for iostream to include the file that has the definition of
NULL. I don't feel like digging in the standard to make sure, but this is most likely the case. Which means this may or may not work on all compilers.
EDIT: Pwned by Daved.
ive never had to use cstddef so since i usually have iostream already included i dont have to do anything else.. if i ever need NULL and dont have/need iostream then as daved said earlier 0 is the easiest and most portable way.
Including cstddef and using NULL is more readable in some cases, and same portability. Using 0 and NULL is the same, but using NULL just by including <iostream> is non standard. Stick to the standard when you can, makes life easier for everyone, besides the extra effort of typing out #include <cstddef> is nothing compared to debug time in a programs lifetime.
rather than having to include a whole header you dont need, would it also be ok to just #define it yourself?
No, because you could easily get multiple definition warnings that are extremely annoying.
but if i dont have a header file that has defined it previously, ie if i dont #include<cstddef> then its not defined, so if i use #define for NULL then itd be the first time the compiler would 'see' it... no?
You'd what to put include gaurds in. Header files are ment to work with more than one sourse file, so just because one sourse file doesn't define null, dosen't mean another will.
You'd have to do this:Code:#ifndef NULL
#define NULL ((void *) 0)
#endif
This is C++ NULL should be defined as 0... see my above post (near the start of this thread).
i was looking in the stddef.h file (where this is actually defined) and it looks like they have both defined, so he just missed the #define for the value of 0.
edit: oh i see what your saying, for C++ the standard is 0 but for C its ((void*)0)Code:/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else /* __cplusplus */
#define NULL ((void *)0)
#endif /* __cplusplus */
#endif /* NULL */
Right, I was just making sure that he used the right one, the one he posted is the C style.
Of course, why would you go through any of that trouble when you should just use 0 anyway.
Luckily, in a few years everybody will start using nullptr instead so at hopefully at some point it will be consistent.