Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Declare the given statement in h file and initialize in cpp file
Forum Updated to NodeBB v4.3 + New Features

Declare the given statement in h file and initialize in cpp file

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.2k Views 4 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by
    #1

    I am writing a program using qt creator to initialize a session in tensor flow.
    Please consider the following statement:
    std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
    I have to initialize the following separately in header file and later initialize in cpp file.
    My problem is i have to run this created session multiple times.
    But if i declare this sentence in a function it cannot be shared by other function and if i call the same function the session gets initialized again and again creating an unncessary overhead.

    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      What about putting it in a class and use that use that class the other places ?

      KiraK 1 Reply Last reply
      0
      • KiraK Kira

        I am writing a program using qt creator to initialize a session in tensor flow.
        Please consider the following statement:
        std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
        I have to initialize the following separately in header file and later initialize in cpp file.
        My problem is i have to run this created session multiple times.
        But if i declare this sentence in a function it cannot be shared by other function and if i call the same function the session gets initialized again and again creating an unncessary overhead.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        Good ol' extern doesn't work?
        .h

        extern std::unique_ptr<tensorflow::Session> session_inception;
        

        .cpp

        std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
        

        Read and abide by the Qt Code of Conduct

        KiraK 1 Reply Last reply
        1
        • kshegunovK kshegunov

          Good ol' extern doesn't work?
          .h

          extern std::unique_ptr<tensorflow::Session> session_inception;
          

          .cpp

          std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
          
          KiraK Offline
          KiraK Offline
          Kira
          wrote on last edited by
          #4

          @kshegunov said in Declare the given statement in h file and initialize in cpp file:

          .

          @kshegunov : Actually i tried it earlier it gives the declaring it in the header files give me following error:
          error: C2071: 'MainWindow::session_inception': illegal storage class

          kshegunovK 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            What about putting it in a class and use that use that class the other places ?

            KiraK Offline
            KiraK Offline
            Kira
            wrote on last edited by
            #5

            @mrjj : Hi if i create a class i still have to declare it and initialize it.
            I just want to maintain the lifecycle so that i don't need to initialize the
            session again

            1 Reply Last reply
            0
            • KiraK Kira

              @kshegunov said in Declare the given statement in h file and initialize in cpp file:

              .

              @kshegunov : Actually i tried it earlier it gives the declaring it in the header files give me following error:
              error: C2071: 'MainWindow::session_inception': illegal storage class

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Kira said in Declare the given statement in h file and initialize in cpp file:

              Actually i tried it earlier it gives the declaring it in the header files give me following error:
              error: C2071: 'MainWindow::session_inception': illegal storage class

              Well, code doesn't exist in isolation, it has context, and if you had shared that error in the original post we would've known. What my impression was is that this'd been an unscoped global, as it is in a class, it should be static instead of extern.

              .h

              static std::unique_ptr<tensorflow::Session> session_inception;
              

              .cpp

              std::unique_ptr<tensorflow::Session> MainWindow::session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              3
              • KiraK Offline
                KiraK Offline
                Kira
                wrote on last edited by
                #7

                @kshegunov :Thanks for reply, I tried to implement the above it gave me following error:
                error: C2655: 'MainWindow::session_inception': definition or redeclaration illegal in current scop
                error: C2086: 'std::unique_ptr<tensorflow::Session,std::default_delete<_Ty>> MainWindow::session_inception': redefinition
                with
                [
                _Ty=tensorflow::Session
                ]

                1 Reply Last reply
                0
                • KiraK Offline
                  KiraK Offline
                  Kira
                  wrote on last edited by VRonin
                  #8

                  @kshegunov : Thanks for your time.
                  I figured a way to initialize it.
                  .h file: std::unique_ptr<tensorflow::Session> session_inception;
                  .cpp file: session_inception = std::unique_ptr<tensorflow::Session>(tensorflow::NewSession(tensorflow::SessionOptions()));
                  It was a pure c++ question still posted because its the best forum to get helpful
                  response.
                  Thanks once again

                  VRoninV 1 Reply Last reply
                  0
                  • KiraK Kira

                    @kshegunov : Thanks for your time.
                    I figured a way to initialize it.
                    .h file: std::unique_ptr<tensorflow::Session> session_inception;
                    .cpp file: session_inception = std::unique_ptr<tensorflow::Session>(tensorflow::NewSession(tensorflow::SessionOptions()));
                    It was a pure c++ question still posted because its the best forum to get helpful
                    response.
                    Thanks once again

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @Kira said in Declare the given statement in h file and initialize in cpp file:

                    session_inception = std::unique_ptrtensorflow::Session(tensorflow::NewSession(tensorflow::SessionOptions()));

                    You can simplyfy to session_inception.reset(tensorflow::NewSession(tensorflow::SessionOptions())); no need to call the move constructor

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    3

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved