Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.


  • Lifetime Qt Champion

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


  • Qt Champions 2017

    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()));
    


  • @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



  • @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


  • Qt Champions 2017

    @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()));
    


  • @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
    ]



  • @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



  • @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


Log in to reply