Support std::shared_ptr<>



  • Hello C++ Gurus! I'm new in qt programming, and have next problem.
    I have c++ library, that I want to use in qt application. This library was created in VS2010 and uses in another applications. But there is problem in qt application when I use it, because one of the imported classes contains std::shared_ptr<> member. When compiling header of this class I get next errors( build in qt-creator, Qt 4.7.4):

    ISO C++ forbids declaration of 'shared_ptr' with no type
    invalid use of '::'
    expected ';' before '<' token

    I was checked a support of std::shared_ptr directly in qt application, and got same errors.

    Is a way to support using std::shared_ptr in current or future version of qt creator?
    What you can suggest to solve this problem: build qt application with c++11x features?



  • Not an issue of Creator, but it seems you're missing

    @
    #include <memory>
    @



  • Unfortunately, do not missed.
    As I said, this code builds and works fine in another application.
    I think, the problem in the compiler, and will be greate if somebody will tell me what exactly is wrong.



  • std::shared_ptr is only available in compilers that support the C++ 11 standard. If it is the same compiler as the other application, it's likely that you need to enable the C++ 11 features with a compiler switch or project setting.



  • How can I enable C++ 11 features in the Creator, and its compiler? Mingw 4.4 as I think...



  • To enable C++11 support for MinGW 4.4 add <code>QMAKE_CXXFLAGS += -std=gnu++0x</code> to your .pro file.

    Be aware that you usually cannot mix different compilers resp. compiler versions. If your library is compiled using MSVC2010 you will have to use MSVC2010 for your application as well.



  • Lukas Geyer - thanks, i'll try.
    I saw this method, but thought, that it is not native.

    Can you give a link to a really WORKING tutorial, how to create/build qt application, using VS2010.



  • The same way you build applications with MinGW. Make sure you've installed the Qt libraries for MSVC2010 (using the SDK maintenance utility) and the MSVC2010 compiler itself (by either installing the Windows SDK or VisualStudio 2010, which comes with the latest version of MSVC2010) and select the MSVC2010 toolchain in Qt Creator instead of the MinGW toolchain.



  • Why not use QSharedPointer instead? That will compile at least even without C++11 support...



  • I use shared_ptr in qt creator. and it works fine.
    but qt creator doesn't recognize the identifier in the editor.
    so no highlights, no hints.
    I guess its because it doesn't understand what is @QMAKE_CXXFLAGS += -std=c++11@ and thereby doesn't define @GXX_EXPERIMENTAL_CXX0X@ in its internal parser.

    PS I don't use QSharedPointer in this particular case because I need library w/o qt dependencies.

    [Edit: Wrapped in @ tags to prevent incorrect formatting; mlong]



  • oops underscores are converted to formatting ))



  • The syntax highlighter finds std::shared_ptr if I add

    @DEFINES += GXX_EXPERIMENTAL_CXX0X@

    to my .pro file.

    I am using the latest build (2.6.0-rc), but this may still work for older builds?


  • Moderators

    [quote author="Sergey I." date="1338983415"]qt creator doesn't recognize the identifier in the editor.
    so no highlights, no hints.
    I guess its because it doesn't understand what is @QMAKE_CXXFLAGS += -std=c++11@ and thereby doesn't define @GXX_EXPERIMENTAL_CXX0X@ in its internal parser.[/quote]

    [quote author="Saul" date="1352166263"]The syntax highlighter finds std::shared_ptr if I add

    @DEFINES += GXX_EXPERIMENTAL_CXX0X@

    to my .pro file.[/quote]

    This sounds like a bug or missing feature in Qt Creator. You can report it at https://bugreports.qt-project.org/secure/Dashboard.jspa , so that the Qt Creator team can fix it.



  • [quote author="JKSH" date="1352201290"]
    This sounds like a bug or missing feature in Qt Creator. You can report it at https://bugreports.qt-project.org/secure/Dashboard.jspa , so that the Qt Creator team can fix it.[/quote]

    There appears to be an existing bug report on exactly this problem, created 05/Oct/11: https://bugreports.qt-project.org/browse/QTCREATORBUG-6239

    It also notes the same workaround to allow auto-completion to work properly. The same problem exists in the latest eclipse IDE (juno), which is why I knew the solution.


  • Moderators

    Thanks for bringing that issue to my attention: I fixed that a while back and thus resolved the issue:-)

    We do take compiler flags into account for a while now (incl. -std=Whatever) when querying the compiler for its defines. std::shared_ptr<int> is highlighted for me out of the box.



  • That's strange, because I am using the latest build, 2.6.0-rc, and no highlighting of std::shared_ptr was working, even though it compiled correctly i.e. I did have -std=c++11 set. I am using Ununtu 12.10, g++ 4.7.2.

    ...

    Actually, just tested by creating a new project from template and added -std=c++11, all works, highlighting and accessing members through arrow access on the shared_ptr.

    Never mind, my bad.

    Not sure then why I had trouble when I first started my other projects (I am new to Qt Creator) -- perhaps needed to restart, or perhaps it takes a while to parse the full project to allow access to members?

    Thanks.


  • Moderators

    Maybe it is the @DEFINES += GXX_EXPERIMENTAL_CXX0X@ workaround?



  • sorry to revive this old post, but I'm having the same problem with
    std::shared_ptr

    the syntax is not recognized by QtCreator, alltought the code compiles fine
    and std::unique_ptr works fine.
    what is weird is if I press f2 on unique_ptr, I can see the code for shared_ptr in the same class <memory> but still the syntax is not recognized,
    it's kind of irritating because the indenting is all messed up whenever I used shared_ptr

    Using QtCreator 5.1.2 with MSVC2010


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.