0

I have following code, which has circular dependency as well as each class use a different name space. I am getting the following compilation error. I am not sure what is the correct solution to resolve this error. Tried few things, but unsuccessful. Can someone point the correct code?

Apart from circular dependency, I am more unclear of implications of different namespaces, if any, and how if it affects the dependency in some way.

I have pasted excerpts from the original program files.

 // ndn-nlsr-app.hpp 

 19 #include "model/nlsr/name-prefix-table.hpp"
 21 
 22 namespace ns3 {
 23 namespace ndn {
 24 
 25 class NlsrApp: public App {
 26 
 27 public:
 65   nlsr::NamePrefixTable&
 66   getNamePrefixTable()
 67   {
 68     return m_namePrefixTable;
 69   }
 70   
103   nlsr::NamePrefixTable m_namePrefixTable;
108 };
109 
110 }//namespace ndn
111 }//namespace ns3

 // name-prefix-table.hpp 

  9 #include "apps/ndn-nlsr-app.hpp"
 10 namespace ns3 {
 11 namespace nlsr {
 12   
 13 class NamePrefixTable
 14 { 
 15 public:
 16   NamePrefixTable(ndn::NlsrApp& nlsr)
 17     : m_nlsr(nlsr)
 18   {
 19   }
 40 private:
 41   ndn::NlsrApp& m_nlsr;
 42 };
 44   
 45 }//namespace nlsr
 46 }//namespace ns3 


In file included from ../src/ndnSIM/apps/ndn-nlsr-app.hpp:19:0,
                 from ../src/ndnSIM/apps/ndn-nlsr-app.cpp:1:
../src/ndnSIM/model/nlsr/name-prefix-table.hpp:17:31: error: expected ‘)’ before ‘&’ token
../src/ndnSIM/model/nlsr/name-prefix-table.hpp:42:3: error: ‘NlsrApp’ in namespace ‘ns3::ndn’ does not name a type

In file included from ../src/ndnSIM/model/nlsr/name-prefix-table.hpp:9:0,
                 from ../src/ndnSIM/model/nlsr/name-prefix-table.cpp:5:
../src/ndnSIM/apps/ndn-nlsr-app.hpp:65:3: error: ‘NamePrefixTable’ in namespace ‘ns3::nlsr’ does not name a type
../src/ndnSIM/apps/ndn-nlsr-app.hpp:103:3: error: ‘NamePrefixTable’ in namespace ‘ns3::nlsr’ does not name a type
AnilJ
  • 1,951
  • 2
  • 33
  • 60
  • 3
    Please don't include line-numbers when posting code, it should not be needed if you mark problematic code with e.g. comments, and it also makes copying the code (for e.g. online compilers etc.) much harder. – Some programmer dude Apr 21 '15 at 08:25
  • 2
    simply use a forward declaration for a class for which only a pointer or reference is used, such as your `ndn::NlsrApp`. – Walter Apr 21 '15 at 08:26
  • 2
    You can put forward declarations inside a namespace, e.g. `namespace ns3 { namespace ndn { class NlsrApp; }}`. – molbdnilo Apr 21 '15 at 08:34
  • After adding forward deceleration in both the classes, I still get the following error. In file included from ../src/ndnSIM/apps/ndn-nlsr-app.hpp:20:0, from ../src/ndnSIM/model/nlsr/name-prefix-table.hpp:7, from ../src/ndnSIM/model/nlsr/name-prefix-table.cpp:5: ../src/ndnSIM/model/nlsr/lsdb.hpp:23:20: error: expected ‘)’ before ‘&’ token ../src/ndnSIM/model/nlsr/lsdb.hpp:222:3: error: ‘NlsrApp’ in namespace ‘ns3::ndn’ does not name a type – AnilJ Apr 21 '15 at 09:06
  • You must, of course, make the forward declaration inside the appropriate namespace. – Walter Apr 21 '15 at 12:29

0 Answers0