-
Little C++ question
I've just migrated to c++ from java. Just a little question here...
I am using Borland c++ 5.02 to compile my stuff, and when I type
std::cout << std::endl;
as stated in my book it does not compile.
but if I type
cout << endl;
it does... what is the std thing and how come I don't need to type it in Borland? I know this is pretty basic but am just curious.
Pier.
-
std is the standard namespace, where the cout and cin and the other functions that are found in the iostream header file are found. The '::' is the scope resolution operator which lets the compiler know that the function to the right is found within the namespace,class,struct, to the right( it brings the object into scope). The reason you do not need to type this is most likely because the header file you are using already brings the namespace into scope. For further explainations just search for namespace and scope on this board. You could even try the tutorials.
-
Are there namespaces in Java? STD is a standard namespace. A namespace is simply a structure(if even considered one) used for organization. It stores variables in a common area.
Code:
#include <iostream>
namespace foo
{ int endl=5;}
int main()
{ std::cout<<std::endl;//will output a new line
std::cout<<foo::endl;//will output 5
return 0;
}
P.S. Your problem is probably that you included .h after iostream. This is the older header file convention and is still widely used. You have to either drop the .h and use std:: (could also put using namespace std; right below your header files. if you did, you wouldn't need the std::) or keep the .h after the header files.
P.S.S. As you get more advanced, you will learn that some with the .h and without the .h differ. An example is the string header file. So be careful on which one u use. (iostream doesn't have this problem)
-
The problem is the code below is all I typed!
I didn't use any header file, or did borland c++ automatically use it? If so, how do I disable it? Even when I remove std:: it says that I should use it and that it compiles so that older code can still be used(backward compatibility).
Code:
#include <iostream>
#include <string>
int main() {
{
const std::string s = "a string";
std::cout << s << std::endl;
{const std::string s = "another string";
std::cout << s << std::endl; }
}
std::string name;
std::cin >> name;
return 0;
}
but it gives me the following errors
Info :Compiling C:\DOCUMENTS AND SETTINGS\PIER\MY DOCUMENTS\ACCC++\noname00.cpp
Warn : string.h(549,3):Functions containing for are not expanded inline
Warn : string.h(557,3):Functions containing while are not expanded inline
Warn : string.h(563,3):Functions containing for are not expanded inline
Warn : string.h(575,3):Functions containing for are not expanded inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
Error: noname00.cpp(7,13):'cout' is not a member of 'std'
Error: noname00.cpp(7,32):'endl' is not a member of 'std'
Error: noname00.cpp(9,13):'cout' is not a member of 'std'
Error: noname00.cpp(9,32):'endl' is not a member of 'std'
Error: noname00.cpp(12,12):'cin' is not a member of 'std'
-
When I take off all the std:: , the following error messages appear but the program compiles and runs...
Info :Compiling C:\DOCUMENTS AND SETTINGS\PIER\MY DOCUMENTS\ACCC++\noname00.cpp
Warn : string.h(549,3):Functions containing for are not expanded inline
Warn : string.h(557,3):Functions containing while are not expanded inline
Warn : string.h(563,3):Functions containing for are not expanded inline
Warn : string.h(575,3):Functions containing for are not expanded inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
Warn : noname00.cpp(6,16):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(8,17):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(11,16):Use qualified name to access member type 'std::string'
Info :Linking C:\DOCUMENTS AND SETTINGS\PIER\MY DOCUMENTS\ACCC++\NONAME00.exe
-
dunno..
maybe you could try using
Code:
#include <iostream.h>
instead of just <iostream>
Havent tried this but still.
cin, cout, cerr etc are already in iostream.h , and so you prolly dont need to use
Code:
std:cout << "text";
/* Instead */
cout << "text";
which is what i use and works OK
Yours.
-
It appears that the version of your compiler is not namespace compliant. To check, try this:
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world,";
return 0;
}
If your compiler balks or otherwise gives errors, try this:
Code:
#include <iostream.h>
int main()
{
cout << "hello world";
return 0;
}
If the second version works and the first version doesn't, then the compiler your are using is not namespace compliant. That would mean you need to use the .h extension behind iostream and you can't use syntax: std::whatever or using namespace whatever. Not all versions of VC++ nor all versions of Borland or any other compilers available out there are namespace compliant. The newest versions from the major vendors should be, but anything else is questionable.
Note: one explanation I have seen is that compilers that aren't namespace compliant do everything in namespace std without knowing they are doing so. Sort of like our home addresses usually don't have Earth or Milky Way or Galaxy XY0X122 attached to them. depending on your point of view, at some time in the future the additional address information may be important, but for now who cares. Same with namespaces, for most applications they are irrelevant. For bigger, usually commercial type applications with multiple modules, etc. they can be relevant. If your compiler is compliant so be it. Learn the syntax. If not, so be it. It won't be that difficult to learn the additional lingo once you do have a compliant compiler.
-
I tried it, and I think Borland c++ 5.2 is not namespace compliant... have ditched it and moved on to dev c++ 4 ... so far no problems :)
Pier.
-
Dev-C++ is a good compiler
-
Code:
int main() {
{
const std::string s = "a string";
std::cout << s << std::endl;
{const std::string s = "another string";
std::cout << s << std::endl; }
}
std::string name;
std::cin >> name;
return 0;
}
Why do you have all these wandering curly brackets?
-
first of all dont use dev c++ 5 beta use 4. cause 5 has bugs cause of incompletion and does not work 1--% good so until the complete defv c++ 5 co,mes out use 4. and also use
before
so the program doesnt close before u can read wuts goign on. thropugh time you may not need it though