Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Accessing variables in nested classes

Accessing variables in nested classes

Scheduled Pinned Locked Moved C / C++ / MFC
questionioshelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pl_kode
    wrote on last edited by
    #1

    I am doin the following...

    Class A
    {
    public:
    void out_func1();
    class B
    {
    public:
    string path;
    in_func1(); //Assigning the value of path in func1
    };
    };

    How do I use the value of path in 'out_func1()'?? I tried ...

    void A::out_func1()
    {
    B obj;
    obj.path;
    ofstream myfile;
    myfile.open (obj.path.c_str(), ios::app);
    ...
    }

    This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.

    S C P 3 Replies Last reply
    0
    • P pl_kode

      I am doin the following...

      Class A
      {
      public:
      void out_func1();
      class B
      {
      public:
      string path;
      in_func1(); //Assigning the value of path in func1
      };
      };

      How do I use the value of path in 'out_func1()'?? I tried ...

      void A::out_func1()
      {
      B obj;
      obj.path;
      ofstream myfile;
      myfile.open (obj.path.c_str(), ios::app);
      ...
      }

      This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      It seems you are using it correctly except that you are not setting the file path so the IO to the file will fail. Are you sure program is crashing in this function? Have you actually debugged to see where is the crash? -Saurabh

      1 Reply Last reply
      0
      • P pl_kode

        I am doin the following...

        Class A
        {
        public:
        void out_func1();
        class B
        {
        public:
        string path;
        in_func1(); //Assigning the value of path in func1
        };
        };

        How do I use the value of path in 'out_func1()'?? I tried ...

        void A::out_func1()
        {
        B obj;
        obj.path;
        ofstream myfile;
        myfile.open (obj.path.c_str(), ios::app);
        ...
        }

        This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Well, since you haven't initialized the obj.path variable, not wonder if the application doesn't behave correctly. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        1 Reply Last reply
        0
        • P pl_kode

          I am doin the following...

          Class A
          {
          public:
          void out_func1();
          class B
          {
          public:
          string path;
          in_func1(); //Assigning the value of path in func1
          };
          };

          How do I use the value of path in 'out_func1()'?? I tried ...

          void A::out_func1()
          {
          B obj;
          obj.path;
          ofstream myfile;
          myfile.open (obj.path.c_str(), ios::app);
          ...
          }

          This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.

          P Offline
          P Offline
          pl_kode
          wrote on last edited by
          #4

          I am sorry. This is how it is... My Class is defined in newclass.h as...

          Class A
          {
          public:
          void out_func1();
          void out_func2();
          class B
          {
          public:
          string path;
          in_func1();
          };
          };

          I then Get the path at intialize.cpp as follows....

          void A::out_func1()
          {
          B obj;
          obj.path="c:\folder\file1.txt";
          }

          Then I try to open the file to write in write.cpp...

          void A::out_func2()
          {
          B obj;
          ofstream myfile;
          myfile.open (obj.path.c_str(), ios::app);
          ...
          }

          This crashes.I have included the header initialise.h too. Please let me know were I could have gone wrong. I think I am having problem with the scope of the varible. But I am not sure. THANKS.

          P 1 Reply Last reply
          0
          • P pl_kode

            I am sorry. This is how it is... My Class is defined in newclass.h as...

            Class A
            {
            public:
            void out_func1();
            void out_func2();
            class B
            {
            public:
            string path;
            in_func1();
            };
            };

            I then Get the path at intialize.cpp as follows....

            void A::out_func1()
            {
            B obj;
            obj.path="c:\folder\file1.txt";
            }

            Then I try to open the file to write in write.cpp...

            void A::out_func2()
            {
            B obj;
            ofstream myfile;
            myfile.open (obj.path.c_str(), ios::app);
            ...
            }

            This crashes.I have included the header initialise.h too. Please let me know were I could have gone wrong. I think I am having problem with the scope of the varible. But I am not sure. THANKS.

            P Offline
            P Offline
            pl_kode
            wrote on last edited by
            #5

            Please help me out with the above problem. THANKS.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups