This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Problems getting g++ to run
"Karl Gangle" <kgangle72 at attbi dot com> writes:
> Hi,
>
> I am trying to compile a c++ program using g++ in Linux. The program is
>
> #include <iostream>
> #include <string>
>
> int main() {
>
> string s1( "string1" );
>
> cout << s1 << endl;
>
> return 1;
> }
>
>
> The compiler errors indicate that string, cout, endl, and s1 are not
> defined.
And, indeed, they are not. All those identifiers are inside std
namespace, like all other C++ Standard Library names, so just prepend
them with std::
#include <iostream>
#include <string>
int main() {
std::string s1( "string1" );
std::cout << s1 << std::endl;
return 1;
}
If you don't want to type lots of std::'s, you can use this too,
although is considered a bad practice by lots of gurus:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1( "string1" );
cout << s1 << endl;
return 1;
}
[snip]
--
Oscar