using namespace std;
The tutorials I get sent to often have this in them, but when i try to use this my compiler says, "Namespace name expected" ?
Printable View
using namespace std;
The tutorials I get sent to often have this in them, but when i try to use this my compiler says, "Namespace name expected" ?
#include <iostream> // does not work
using namespace std; // does not work
try
#include <iostream.h> // this is the old style. So could solve ur problem
http://cboard.cprogramming.com/showt...sing+namespace
edit: post some code, it should work. also what compiler?
Code:#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello World\n";
}
Borland C++ 5.02
Either say
orCode:#include <iostream.h>
int main()
{
cout << "Hello World" << endl;
return 0;
}
or even betterCode:#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
Note: One of the members provided a link, please visit that. It will explain a lot of details about namespacesCode:#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
Try #include <iostream>
This does not work either:
I went to the link and I still don't see whats wrong
Code:#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
}
Nor does this work as std is not a class or namespace and there is a statement missing
Code:#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
I have posted 3 forms of the program, you have tried only the 2nd and the 3rd. Try the first form. It must workQuote:
Originally posted by Noobie
Nor does this work as std is not a class or namespace and there is a statement missing
Code:#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
Note: I don't use namespace at all in the first form
but why? am i not supposed to ever use namespace? why is it in all the tutorials? Some of them call for
#include "string6.h" what is that? it doesnt work either
Chill out :)Quote:
Originally posted by Noobie
but why? am i not supposed to ever use namespace? why is it in all the tutorials? Some of them call for
#include "string6.h" what is that? it doesnt work either
The compiler that you are using might have been developed when namespaces were not existing as part of C++. So your compiler does not have most of the recently added features / libraries of C++. So just change to the latest compilers and your problem would be solved.
w.r.t namespaces
When you say #include<iostream.h>
everything is loaded into the global namespace at one shot
If you had split your contents into multiple sections (lets say std1, std2, std3, std4) and didn't want everything to be stuffed in the global namespace, then you could say
using namespace std1; and that would load only what is there in std1 in the global space and will not load std2 / 3/ 4 in the global namespace.
Having said that std1/2/3/4 was just to help you understand. There are no such namespaces available.. unless you create any with those names
so i can only do tutorials written last year??
the above posted should work. that's weird. the "string6.h" is a different header file, most likely for a class.
for the "but why? am i not supposed to ever use namespace?", you could do this either of three ways...
using namespace std;
OR
using std::cout;
using std::endl;
//continue with more if necessary
OR
std::cout << std::endl;
//like in previous examples
using std::cout;
using std::endl;
//continue with more if necessary
OR
std::cout << std::endl;
uh... :< it seems pretty difficult to explain that Im a noobie asking for help on a HelloWorld compilation-i dont know what std is and I dont know what all that code u are telling me I could use is and I can't read some tutorial to find out when the tutorials dont even work
Ok, i'l explain.
A namespace is like a word before every function in it to avoid
that it has the same name as another function.
demonstartion, whe want 2 integers, and whe want both of
them to be called name,but thats not possible so whe use a
namespace.
Easy as that, of course this long from everything but this are justCode:
#include <iostream>
using namespace std;
namespace tom
{
int age=11;
}
namespace pete
{
int age=14;
}
int main()
{
cout << tom::age; // Print the age int stored in the namespace tom
cout << pete::age; // Print the age int stored in the namespace pete
return 0;
}
the fundementals.
As for the 'using namespace std;' , that just means you dont have to put 'std::' before everything you want to use for that namespace.
so since i cant use namespace std; where would i put std in the above program?
Include all your headerfules with ".h"Quote:
Originally posted by Noobie
so since i cant use namespace std; where would i put std in the above program?
and just don't add using namespace std
in that case you can use it without prefixing it with std
for example
Code:#include <iostream.h>
#include <string.h>
int main()
{
cout << "I have not used std anywhere and it still works" << endl;
cout << "Its outdated ofcourse " << endl;
return 0;
}
The above worked
Atlast...... It did :) Wooofffff!!!!!!!!!!! I need a breather :)Quote:
Originally posted by Noobie
The above worked
great to hear it worked. my previous post was so you are at least exposed to it. just remember, that iostream.h is nonstandard and newer compilers will give you warnings for it.
are you using a c++ compiler or a vc++ compiler?
You do realize this thread is 2½ years old, don't you?Quote:
Originally Posted by fingerlickin
Oh, MS VC++ 6.0
oops, wrong thread...funny thing answer is related.