簡體   English   中英

C ++命名空間麻煩

[英]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;

...

此外,portl.cpp文件包含在其他“主要”源文件中。 並且此“其他主”文件也包含con.cpp。 因此,如果我將con.cpp包含在portl.cpp中,則將連接器定義兩次(在portl和main中)。 如果不包括在內,編譯器將不知道Connecter :: ConPtr(或FAWN :: sys :: Connecter :: ConPtr)的含義,並嘗試將其用作方法的定義。

class Connecter (您可能應該將其重命名為Connector )放入頭文件( .h而不是.cpp )中,並將包含保護措施添加到文件中。 也就是說,在con.h文件的開頭,添加行

#ifndef CON_H_INCLUDED
#define CON_H_INCLUDED

最后添加一行

#endif

這樣,即使您兩次#include con.h ,第二次也不會被讀取,因為第一次定義了符號CON_H_INCLUDED因此#ifndef-#endif對隱藏了內容。

這是C ++中的常用方法:將類聲明放入.h文件中,然后將#include d放入.cpp文件中,然后再實際定義函數。

這是它的外觀:

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM