Problems getting g++ to run

Oscar Fuentes ofv@wanadoo.es
Wed Mar 5 23:33:00 GMT 2003


"Karl Gangle" <kgangle72@attbi.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



More information about the Gcc-help mailing list