嵌套类崩溃C ++

时间:2015-10-27 11:14:02

标签: c++ class nested

Main.cpp的

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel2"
                            DisplayAfter="1">
            <ProgressTemplate>
                <div id="IMGDIV" runat="server" align="center" style="left: 55%; visibility: visible;
                                    vertical-align: middle; position: absolute; top: 290%; background-color: #fafbf6"
                                    valign="middle">
                    <asp:Image ID="Image001" runat="server" ImageUrl="images/loading_bar_animated.gif" />
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>

Nationality:<asp:DropDownList ID="ddl_Nationality" runat="server" CssClass="dropdown" AppendDataBoundItems="true"
            AutoPostBack="true" OnSelectedIndexChanged="ddl_Nationality_SelectedIndexChanged">
                <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                    <asp:ListItem Text="Indian" Value="Indian"></asp:ListItem>
                    <asp:ListItem Text="Expatriate" Value="Expatriate"></asp:ListItem>
          </asp:DropDownList>


    Title: <asp:DropDownList ID="ddl_Title" runat="server" CssClass="dropdown" AppendDataBoundItems="true">
    <asp:ListItem Text="Select" Value="0"></asp:ListItem>
        <asp:ListItem Text="Mr." Value="Mr."></asp:ListItem>

        </asp:DropDownList>

    Name:  <asp:TextBox ID="txt_Name" runat="server" CssClass="textfield_new1" MaxLength="20"></asp:TextBox>

    </ContentTemplate>
    </asp:UpdatePanel>


     <asp:UpdatePanel ID="UpdatePanel3" runat="server">
      <ContentTemplate>

    <table>
    <tr>
    <td>
    Country:    <asp:DropDownList ID="ddl_Country" runat="server" CssClass="dropdown">
            <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                  </asp:DropDownList>

    </td>

    </tr>

    </table>

    </ContentTemplate>
    </asp:UpdatePanel>


    <table>
    <tr>
    <td>
        <asp:UpdatePanel ID="UpdatePanel5" runat="server" >
                <ContentTemplate>
                   Total Experience:        <asp:DropDownList ID="ddl_years" runat="server" AutoPostBack="true" CssClass="Exp"
                   OnSelectedIndexChanged="ddl_years_SelectedIndexChanged">
                       <asp:ListItem Value="0">Year</asp:ListItem>
               <asp:ListItem Value="1">0</asp:ListItem>
                    <asp:ListItem Value="2">1</asp:ListItem>
                        <asp:ListItem Value="3">2</asp:ListItem>
                        <asp:ListItem Value="4">3</asp:ListItem>

             </asp:DropDownList>
                  <asp:DropDownList ID="ddl_months" runat="server" CssClass="Exp"
               Enabled="false">
                   <asp:ListItem Selected="True" Value="0">Month</asp:ListItem>
              <asp:ListItem Value="1">0</asp:ListItem>

              </asp:DropDownList>
               </ContentTemplate>
                </asp:UpdatePanel>
             </td> </tr>

    </table>


        <table>
    <tr>
    <td>
            <asp:UpdatePanel ID="UpdatePanel4" runat="server">
                <ContentTemplate>
        <cc1:CaptchaControl ID="CaptchaControl1" runat="server" CaptchaBackgroundNoise="None"
            CaptchaHeight="40" CaptchaLength="5" CaptchaLineNoise="None" CaptchaMaxTimeout="240" 
        CaptchaMinTimeout="5" FontColor="111, 109, 85" Width="325px" CaptchaWidth="300"
        CssClass="textarea_captha" />

            </ContentTemplate>
        </asp:UpdatePanel>

    </td></tr>
        <tr><td>
    Enter Text as you See:<asp:TextBox ID="txtCaptcha" runat="server" CssClass="textfield_new1"></asp:TextBox>

        </td></tr>

    </table>

    <table>
    <tr><td>

    <asp:LinkButton ID="imgbtn_Submit" runat="server" ValidationGroup="reg" CssClass="submitbtn"

    </td></tr>
    </table>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
                <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/spacer.gif" />
                <cc1:ModalPopupExtender ID="ModalPopupExtender1" TargetControlID="ImageButton1" PopupControlID="Pnl_popup1"
                BackgroundCssClass="modalBackground" Drag="true" PopupDragHandleControlID="Pnl_popup1"
                                                    runat="server">
                    </cc1:ModalPopupExtender>
                    <asp:Panel ID="Pnl_popup1" runat="server">
                                                //pop design code is here

                                                    </asp:Panel>
                </ContentTemplate>
                </asp:UpdatePanel>

Test.cpp的

#include <string>

#include "Test.h"
#include "Test.cpp"

using namespace std;
using namespace Classes;

int main(int argc, char** argv) {

    Test test("bar");   

    return 0;
}

Test.h

#include "Test.h"

namespace Classes {

    class Test::Implementation {
        string mFoo;
        friend class Test;
    };

    Test::Test(string foo) {
        setFoo(foo);
        i = new Test::Implementation();
    }

    Test::~Test() {

    }

    string Test::getFoo() {
        return i->mFoo;
    }

    void Test::setFoo(string foo) {
        i->mFoo = foo;
    }
}

我正在尝试使用C ++中的嵌套类。 当我编译此应用程序时,我遇到一个问题:“Main.exe已停止工作” 我找不到问题。但我知道我的应用程序崩溃然后我尝试#ifndef TEST_H #define TEST_H using namespace std; namespace Classes { class Test { private: class Implementation; Implementation *i; public: friend class Implementation; Test(string foo); ~Test(); string getFoo(); void setFoo(string foo); }; } #endif 。也许有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

Test::Test()构造函数中,您在初始化setFoo()之前调用了i,因此i在此时未初始化并尝试取消引用未初始化的指针导致崩溃。只需交换这两行,就可以先i初始化。

您还需要将delete i;添加到Test::~Test()析构函数中,否则i的内存将被泄露。