I remember that ns2 has some problems with gcc4. You could give a look at the list of changes to see whether there's some problem in your code that arose with gcc4 (
http://gcc.gnu.org/gcc-4.3/changes.html).
You should also remember to re-run the "configure" scripts when changing the compiler.
If everything fails, you can install a copy of gcc3 and use that one, using the sources downloadable from gcc's site, is not that difficult to rebuild gcc if you're used to build ns.