Click to See Complete Forum and Search --> : Help with GCC / C++


Tulluin
10-19-2002, 09:16 PM
Up until RedHat 8.0 (I was in 7.3) I was using GCC 2.96 to compile my C++ programs for class. It worked fine for my relatively simple programs. Now, in RedHat 8, I have a newer version of GCC, and it says things like:

channels.cc: In function `int main()':
channels.cc:11: `string' undeclared (first use this function)
channels.cc:11: (Each undeclared identifier is reported only once for each
function it appears in.)
channels.cc:11: parse error before `,' token
channels.cc:14: `cin' undeclared (first use this function)
channels.cc:15: `cout' undeclared (first use this function)
channels.cc:15: `endl' undeclared (first use this function

I was told I have to use declarations like this now:

using std::cout;
using std::cin;
using std::endl;

I also can't declare global variables anymore.

Is there a way to change this so I don't have to use the using std: statements, and I can declare global variables, without having to go back to an older gcc? The dependancies are crazy.

monkeyboi
10-20-2002, 01:50 AM
for c++ try using the g++ instead of gcc

Tulluin
10-20-2002, 02:05 AM
I used g++ =(

truls
10-20-2002, 06:12 AM
Could you post the code?
And it's "using namespace std;".

bwkaz
10-20-2002, 10:13 AM
But "using namespace std;" effectively imports everything in the std namespace into the default namespace, which is horrible clutter, IMHO. The way I see it is, one "using std::c<whatever>;" line is one heck of a lot easier than replacing every instance of cout with std::cout, and it's almost as clean on the namespace. But whatever. "using namespace std;" will let you reference anything in the "<iostream>" header file (or any other standard C++ header, actually) just like your older C++ compiler would let you.

The reason this is happening is that that's the way the ISO C++ standard is written -- if you use the new <iostream> header (and the <iostream.h> header has been actively deprecated in g++), that header will not pollute the default namespace with its contents. So if you need to use its contents as if they were in the default namespace, you have to do either "using namespace std;" to pull it all in, or "using std::cout;" or similar to pull in the symbols you need.

The "string" class is also in the std namespace, so replacing "string" with "std::string" will fix that. Or, you can do a "using std::string;", or even a "using namespace std;" (if you solve the iostream problem like this, then the string problem will go away as well).

I also can't declare global variables anymore.Once you've fixed the above problems, does this one go away? If not, post the code and the error message.

Tulluin
10-20-2002, 11:33 AM
Using namespace std fixed my problem. Thanks guys.