Ben Usman
Ben Usman

Reputation: 8387

C++ namespaces trobles

//portl.cpp
namespace FAWN {
namespace Sys{

class PortListner {

....
Connecter::ConPtr _cur_con; - the main problem is here

...

//con.cpp
namespace FAWN {
namespace Sys {

class Connecter {

.....
public:

 typedef boost::shared_ptr<Connecter> ConPtr;

...

Moreover, portl.cpp file is included into some other "main" sourse file. And this "other-main" file includes con.cpp too. So if I include con.cpp to portl.cpp - I define Connecter twice (in portl and in main). If I do not include it, compilator doesn't know what Connecter::ConPtr (or FAWN::sys::Connecter::ConPtr) means and try to use it as defenition of method.

Upvotes: 0

Views: 294

Answers (2)

Igor Zevaka
Igor Zevaka

Reputation: 76590

Here is how it should look:

#ifndef PORTAL_H
#define PORTAL_H
#include "con.h"
//portl.h
namespace FAWN {
namespace Sys{

  class PortListner {

....
    //might need to specify Connector's namespace  fully here
    FAWN::Sys::Connecter::ConPtr _cur_con; 
...
  };
}
#endif //PORTAL_H

//con.h
#ifndef CON_H
#define CON_H
namespace FAWN {
namespace Sys {

  class Connecter {

  .....
  public:

    typedef boost::shared_ptr<Connecter> ConPtr;

  };
}
#endif //CON_H

Upvotes: 1

JaakkoK
JaakkoK

Reputation: 8387

Put the class Connecter (which you should probably rename to Connector) into a header file (.h instead of .cpp) and add include guards into the file. That is, at the beginning of your con.h file, add lines

#ifndef CON_H_INCLUDED
#define CON_H_INCLUDED

and at the very end, add the line

#endif

This way, even if you #include con.h twice, the second time it will not get read because the symbol CON_H_INCLUDED has been defined on the first time so the #ifndef-#endif pair hides the content.

This is the common way in C++: put class declarations in .h files that get #included in .cpp files that then actually define the functions.

Upvotes: 2

Related Questions