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. Problem with variables

Problem with variables

Scheduled Pinned Locked Moved C / C++ / MFC
c++ioshelp
6 Posts 6 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 had posted this earlier but no reply. Sorry for posting it again 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.

    C N _ P D 5 Replies Last reply
    0
    • P pl_kode

      I had posted this earlier but no reply. Sorry for posting it again 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.

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      out_func1() and out_func2() uses different instance of the class B. You should use same instance of class B in both functions.

      nave [OpenedFileFinder]

      1 Reply Last reply
      0
      • P pl_kode

        I had posted this earlier but no reply. Sorry for posting it again 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.

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

        Actually you got replies. I repeat myself: no wonder your application crashes, you're passing a uninitialized path to myfile.open. Hint: usage of locals where class member (or method parameter) should be used. :)

        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 had posted this earlier but no reply. Sorry for posting it again 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.

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          Where do you initialize the variable obj? That kept aside you don't call out_func1 that is initializign the obj variable. Initialize and use the same variables.

          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

          1 Reply Last reply
          0
          • P pl_kode

            I had posted this earlier but no reply. Sorry for posting it again 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
            Paresh Chitte
            wrote on last edited by
            #5

            pl_kode wrote:

            obj.path="c:\folder\file1.txt";

            Use below code, obj.path="c:\\folder\\file1.txt"; Regards, Paresh.

            1 Reply Last reply
            0
            • P pl_kode

              I had posted this earlier but no reply. Sorry for posting it again 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.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              pl_kode wrote:

              myfile.open (obj.path.c_str(), ios::app);

              obj has not been initialized.

              "Love people and use things, not love things and use people." - Unknown

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              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