Home Blog Music Papers Photography About Me

A Solution to some C++ Compilation Problems After Upgrading to g++ 3.x

The Problem

You have a C++ program you wrote (or downloaded) that compiled cleanly under earlier versions of gcc/g++, say version 2.95 or similar. You've recently upgraded you system (in my case to SuSE 9.1 with gcc 3.3.3) and now your programs — source unchanged — won't compile and g++ spits out a ton of errors, but you can't see any problem in the code.

For example, in one header for a library of mine, I have the code:

 9: #include <stdio.h>
10: #include <string>
11: #include <cstring>
12: 
13: 
14: class ValuePair {
15: public:
16:     ValuePair();
17:     ValuePair ( const string name );
18:     ValuePair(const string name, const string value);
19:     ~ValuePair() { ; }
20:     string Name() const { return name; }
21:     bool NameIs(const string& n) const { return (name == n); }
22:     string Value() const { return value; }                     
23:     void Set(const string& value);                             
24: private:
25:     string name;
26:     string value;
27: };

Before the upgrade, it compiled cleanly. Now the following errors are reported (only the first few lines are shown; many more are returned):

In file included from ValuePair.C:19:
ValuePair.H:17: error: syntax error before `)' token
ValuePair.H:18: error: syntax error before `,' token
ValuePair.H:20: error: syntax error before `)' token
ValuePair.H:21: error: semicolon missing after declaration of `ValuePair'
ValuePair.H:21: error: syntax error before `&' token
ValuePair.H:21: error: ISO C++ forbids defining types within return type
ValuePair.H:21: error: `bool' is now a keyword
ValuePair.H:21: error: non-member function `ValuePair NameIs(...)' cannot have 
   `const' method qualifier

So what's the problem???

It seems, for reasons unknown to me, most of the standard C++ definitions, such as the string class, are no longer available in the global namespace. Maybe this is intentional, maybe someone just forgot a line in one of the include files. Either way, it seriously breaks a lot of programs. And if you don't know what namespaces are, the solution is far from obvious.

The Solution

After searching around the Web and finding no obvious solution, I somehow remembered a syntax of std::string and the term namespace surfaced in my memory. I tried changing the const string name in line 17 above to const std::string name, and g++ was happy with that line.

So I looked up namespaces in the C++ Primer, 3rd. ed. by Lippman and Lajoie, and found out how to bring the entire std namespace into the global namespace. I added the follow line, after my #include statements, to my source file:

using namespace std;

So the above code is now:

 9: #include <stdio.h>
10: #include <string>
11: #include <cstring>
12: 
13: using namespace std;
14: 
15: class ValuePair {
16: public:
17:     ValuePair();
18:     ValuePair ( const string name );

Now all the compile errors have gone away.

If you have a program comprised of several modules (most sizeable programs do), you will probably have to add this directive to at least every header file that includes standard headers (string, iostream, etc.), an possibly some of the program files, too. One site does suggest that including using namespace std in headers should never be done, but gives no explaination as to why it shouldn't be done. What I do know is that it works for me.

I hope this helps others who are experiencing similar problems. If you found it helpful, consider linking to this page or it's parent index, so more people will be able to find it. Of course comments are welcome. Questions, too; though be warned that I'm not a C++ expert by any means. See below for my email information.



Best viewed with any browser Last update: January 6, 2005
$Revision: 1.3 $
$Id: g++3.html,v 1.3 2005/01/26 01:45:50 andrew Exp andrew $
Andrew Turnquist, andrew@turnquist.name (Click to whitelist yourself)