Click to See Complete Forum and Search --> : C++ problem (newbie)
scifire
08-04-2003, 03:42 PM
on the problem
why this would't compile
#include <iostream>
int main(void)
{
cout << "This is a test" <<endl;
return 0;
}
and this can
#include <iostream>
using namespace std;
int main(void)
{
cout << "This is a test" <<endl;
return 0;
}
???
:confused:
Stuka
08-04-2003, 03:52 PM
C++ is built around namespaces. cout is a member of the std namespace. In your first example, you don't qualify cout with a namespace, so the compiler can't find it. In the second example, you import the entire std namespace into the global namespace, so the compiler is capable of finding cout. A BETTER solution would be: #include <iostream>
int main(void)
{
std::cout << "This is a test" <<endl;
return 0;
} This allows the compiler to find cout, but doesn't 'pollute' your namespace.
hardcore
08-04-2003, 04:57 PM
This is a namespace problem, but an easy way around not using "using namespace bla bla" is to include the .h in you #include 's. For example:
#include <iostream.h>
int main(void)
{
cout << "This is a test" << endl;
return 0;
}
will compile, but you will get warnings about antiquated headers, etc. C++ has been standardized to now use namespaces, so i'd recommend starting out the 'right way' and use namespaces.
Stuka
08-05-2003, 11:07 AM
Using the '.h' version of the headers is identical to the 'using namespace std;' declaration, with the same issues and problems. Both should be avoided. iostream.h, for example, contains the SAME declarations as iostream, but without the namespace std {} block around it.
schmu_20mol
08-09-2003, 12:23 PM
overall i think it's the easiest way to implement the namespace in the top of your code like
#include <iostream>
using namespace std;
int main()
{
cout << "Test" << endl;
return 0;
}
instead of assigning std everytime you need it ...what would basically be a wasting of the namespace idea imho
bwkaz
08-09-2003, 03:32 PM
Originally posted by schmu_20mol
overall i think it's the easiest way to implement the namespace in the top of your code like... Ehh? Your code might be easier to type, but it defeats the entire purpose of having namespaces. The reason they got introduced was that symbols from the standard C++ libraries were polluting the default namespace. Every time you put a "using" directive in, you're repolluting the default namespace.
With your example, every symbol in the std namespace gets pulled right back into the default one, which is where it was before the std namespace was created to pull it out and reduce pollution. You're repolluting the namespace with a TON of symbols you won't ever use.
Better (IMO) is this:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "testing..." << endl;
return 0;
} You don't need the std:: every time you use a class or object, but you also don't pollute the namespace with stuff you'll never use.
Sepero
08-09-2003, 08:24 PM
The easiest way for me to think of namespaces is: sub-inclusionary
First you have to include iostream. This gives you access to include(or use) namespaces.
So, it's like you're first stating which file you need to include. Then you state which of that files members you will be using.
A namespace can have Any name. The "std" namespace is an files standard members.
Citadel
08-12-2003, 04:56 AM
In header files be explicit such as. That way you are not forcing anyone to widen their scope in any implementation file that includes the header:
#include<string>
#include<vector>
void function( std::string, std::vector<std::string>);
In the implementation (.cpp files) this is probably the best way to go because it is convienient and does not pollute the file:
#include "myheader.h"
#include<iostream>
#include<vector>
#include<string>
using std::vector;
using std::string;
void function( string arg1, vector<string> arg2 ) {
//implementation goes here
}
You can only use the global declaration in trivial programs:
using namespace std;
KneeLess
08-13-2003, 02:43 PM
I guess using
#include <iostream.h>
int main()
{
cout << "Goodbye world.\n";
return 0;
}
doesn't work anymore? :p. That's the easiest way, in my opinion.
bwkaz
08-13-2003, 06:58 PM
It still works, but it's been deprecated, and when you compile you get a large, blinking (well, OK, not blinking, but still large), annoying warning that what you're doing has been deprecated.
Support for non-namespaced headers will be removed eventually, too, AFAIK. I just don't know when.