Re: [Dev-C++] Compiling problems with 4.9.7.0 and GCC 3.2
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <cg...@wo...> - 2003-01-16 18:59:39
|
#include <iostream>
using namespace std;
int main()
{
whatever;
}
In short: the line using namespace std; means that from that line till=20
it gets out of scope, whenever you use a function or a variable, if it=20
exists inside that namespace it is what you are willing to use and not=20
any other function or variable with the same name inside another=20
namespace, and you don't need apend the namespace before each of the=20
functions/variables from it you are using. Of course, if you have a=20
function with the same name in scope and declared outside any=20
namespace, then you will have a conflict between them.
The matter is the namespaces: for avoid problems with the possibility=20
that two different library vendors give the same name to some functions,=20
the declare them inside a namespace so whenever you have to use them you=20
need to declare to which namespace the function or member you're using=20
belongs.
So you have a .h form vendor A and another from vendor B
venA.h
namespace vendorA{
int myfunction();
char *mydata;
}
and venB.h
namespace vendorB{
int myfunction();
char *data;
}
So you can say:
I want the function from A, then I type
vendorA::myfunction();
but if you want the other, then you type
vendor::Bmyfunction();
But if you will use only one of them, or one preferably over the other,=20
you can say
using namespace vendorA;
in a place where it is on scope wherever you'll use its functions, that=20
declares that you want use the functions and members of the namespace=20
vendorA while using namespace vendorA; is in scope.
Then myfunction(); will be a call to vendorA::myfunction.
You can either modify that, which makes available all the functions (and=20
variables) inside the namespace, select only some of them, so in the=20
example, you can say:
#include <venA.h>
using namespace vendorA::myfunction();
int another_function_int_main_calls_from_another_source_file(char firstpa=
r){
myfunction();
vendorA::mydata; <-- Here simply mydata would not work, compiler would=20
report undefined variable.
This is a quick explanation as it comes to my mind right now, I hope you=20
understand it, although I've the feeling that it's not the best=20
explanation I could give you. If you want a clearer explanation, ask me=20
and then I'll answer with clearer explanation, this only intends to be a=20
quick explanation for avoid to forget answer waiting for a moment for=20
say things clearer.
Dany P. Wu wrote:
>>-----Original Message-----
>>From: dev...@li...=20
>>[mailto:dev...@li...] On Behalf=20
>>Of Carlos Garc=EDa del Monte
>>Sent: Thursday, 16 January 2003 11:53 p.m.
>>To: dev-cpp-users-list
>>Subject: Re: [Dev-C++] Compiling problems with 4.9.7.0 and GCC 3.2
>>
>>
>>If you use iostream instead iostream.h you must:
>>1) Add using namespace std; before main or as its first line
>> =20
>>
>
>Can you please explain this one to me? What do I have to do, exactly?
>
> =20
>
>>2) Each time you use a function or name from it you have to=20
>>write std::=20
>>just before it, in your example: std::cout
>>
>>The use of iostream.h is considered deprecated, so if you=20
>>want use it it=20
>>would be more comfortable if you add the compiler option=20
>>-Wno-deprecated, so the compiler won't send you a very long warning=20
>>about it.
>> =20
>>
>
>D.
>
>
> =20
>
|