Welcome to LQ.
I don't think ping.cc was meant for a standalone compiling.
'g++ ping.cc' will show all the missing headers : common/agent.h etc.
common/agent.h includes other headers, that has e.g. #include "lib/bsd-list.h".
All this is a job for a Makefile.
( I assume, you mean ping.cc, and not p.cc (Ref. your error.txt.))
Chapter VII.2 ( = 7.2 ) ( All this was written 10 years ago.)
http://www.isi.edu/nsnam/ns/tutorial/nsnew.html
QUOTE : "But first of all, some other files have to be edited
before ns can be recompiled."
I.e. : Usually a new protocol is compiled into the binary 'ns'.
The old 'ping.cc' is dated year 2000, and may not fit into a recent ns-2.xx.
P.S. : Default location for ns2 is /home/<name>/ns2 or similar.
Then all commands can be done as 'unprivileged user'.
"/root/" is not the place. /root/ is not meant to be used for anything.
PPS. : ping.cc is included in all ns2 since 2001:
find ns2/ -name ping.cc
ns-allinone-2.1b9a/ns-2.1b9a/apps/ping.cc
ns-allinone-2.27/ns-2.27/apps/ping.cc
ns-allinone-2.27/ns-2.27/ns-tutorial/examples/ping.cc
ns-allinone-2.31/ns-2.31/apps/ping.cc
ns-allinone-2.33/ns-2.33/apps/ping.cc
ns-allinone-2.34/ns-2.34/apps/ping.cc
ns-allinone-2.35-RC6/ns-2.35/apps/ping.cc
..