What is current version of your compiler.
If you are using ubuntu 12.04, may be you are using gcc-4.6.3.
As ubuntu version is increased, they drop the older version for installation and it is not possible to install it using apt-get.
You have to download the package and install it manually.
Ubuntu maintains the repository and you can download the deb file from
here but I doubt your dependencies will support it.
If you install it using ./configure, make & make install, Do not remove older version. It will create an older version gcc file in /usr/bin.
You can set the path of your needed file and it will shift on your needed gcc version.
If you see in /usr/bin there is file named gcc, it is a linked file (not in centOS), if you follow the path, it is in same directory which is your current version of gcc.
You can set here your compiler if you find gcc-4.2 in /usr/bin but do not do this just use export to use your needed version.
I was telling you how does it work.