5

私はこれを知らなかったことを知りませんでした:)。ここでの同様の質問はあまり役に立ちませんでした。

だからここで私は尋ねています。次のクラスを検討してください。

//in Agent.h
class Agent : public ns3::Object{
private:
//...

    static BaseWifi m_wifi;

//...
};

これは :

//Agent.cpp
BaseWifi temp;
BaseWifi Agent::m_wifi = temp;

これとは大きく異なります:

//Agent.cpp
BaseWifi Agent::m_wifi = BaseWifi();

2番目のアプローチは私にはうまくいきません。なぜ、どのように?

プログラムの奥深くでこの問題に直面したため、これ以上のコードであなたを悩ませたくありません。のコンストラクター内のもの (メンバー)BaseWifiが正しく初期化されていないため、プログラムはセグ フォールトを生成します。これらの初期化されていない内部メンバーが使用されると、セグ フォールトが発生します。

親切なコメントと回答をお寄せいただきありがとうございます。

ps: 実際、この静的メンバーをまだ初期化しておらず、余分な行を削除していたときに、この問題が見つかりました:

BaseWifi temp;

私の、それは私の混乱をさらに追加しました!!!(これは、コンストラクターmain()に何を入れたかに依存する可能性があるため、今のところ気にしないでください)BaseWifi

Update-1: BaseWifi をご覧になりたい方へ:

class BaseWifi {
    ns3::WifiHelper m_wifiHelper; // a wifi helper apply to setup vehicles Wifi

    ns3::NqosWifiMacHelper m_wifiMacHelper; // a wifi mac helper apply to setup vehicles Wifi

    ns3::YansWifiPhyHelper m_wifiPhyHelper; // a wifi phy helper apply to setup vehicles Wifi

    std::string m_phyMode;

    double m_rss;  // -dBm

    bool m_init_done;

public:

    BaseWifi();

    virtual void init();

    ns3::NetDeviceContainer Install(ns3::NodeContainer &c);

    virtual ~BaseWifi();
};

BaseWifi::BaseWifi() {
    m_init_done = false;
    m_rss = -80;
    m_phyMode ="DsssRate1Mbps";
    // TODO Auto-generated constructor stub
    init();
}

void BaseWifi::init() {
    NS_LOG_UNCOND("inside BaseWifi::init()");
      m_wifiHelper.SetStandard (ns3::WIFI_PHY_STANDARD_80211b);

      m_wifiPhyHelper =  ns3::YansWifiPhyHelper::Default ();

      // This is one parameter that matters when using FixedRssLossModel
      // set it to zero; otherwise, gain will be added
      m_wifiPhyHelper.Set ("RxGain", ns3::DoubleValue (0) );

      // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
      m_wifiPhyHelper.SetPcapDataLinkType (ns3::YansWifiPhyHelper::DLT_IEEE802_11_RADIO);

      ns3::YansWifiChannelHelper wifiChannel;

      wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");

      // The below FixedRssLossModel will cause the rss to be fixed regardless
      // of the distance between the two stations, and the transmit power
      wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",ns3::DoubleValue (m_rss));

      m_wifiPhyHelper.SetChannel (wifiChannel.Create ());

      // Add a non-QoS upper mac, and disable rate control
      m_wifiMacHelper = ns3::NqosWifiMacHelper::Default ();

      m_wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                                    "DataMode",ns3::StringValue (m_phyMode),
                                    "ControlMode",ns3::StringValue (m_phyMode));
      // Set it to adhoc mode
      m_wifiMacHelper.SetType ("ns3::AdhocWifiMac");

      m_init_done = true;
}

//Install the class's embedded settings on the nodes in this node container.
ns3::NetDeviceContainer BaseWifi::Install(ns3::NodeContainer &nc) {
    return m_wifiHelper.Install(m_wifiPhyHelper, m_wifiMacHelper, nc);
}
4

2 に答える 2

8

私は以前にこのような問題に遭遇したことがあります。どうやら、静的メンバー オブジェクトの初期化は、実装がコード内のどこで行われるか、および (おそらく) 全体がどのようにコンパイルされるかに大きく依存します。私が(どこかで)見つけた問題の解決策は、すべてを次のような静的メンバー関数にラップすることでした。

//in Agent.h
class Agent : public ns3::Object{
    private:
    //...

    static BaseWifi& m_wifi();
    //...
};

と:

//in Agent.cpp
BaseWifi& Agent::m_wifi() {
    static BaseWifi TheObject=BaseWifi();
    return TheObject;
}

このようにして、静的メンバー関数が最初に呼び出されたときにオブジェクトが適切に初期化されます。

于 2013-08-13T11:56:15.817 に答える
0

ここでの違いは、最初のアプローチではコピー コンストラクターを使用してオブジェクトを初期化し、2 つ目のアプローチでは既定のコンストラクターを使用することです。

于 2013-08-13T13:01:02.670 に答える