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. std::vector reference error C2440
Forum Updated to NodeBB v4.3 + New Features

std::vector reference error C2440

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.1k 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.
  • M Offline
    M Offline
    mireiner
    wrote on 4 Dec 2018, 13:34 last edited by mireiner 12 Apr 2018, 13:36
    #1

    Hi there,

    I'm using Qt 5.9.7 with Visual Studio 2017 64bit compiler or alternative Visual Studio 2017 with Qt Viusal Studio Tools 2.2.2 programming Windows desktop application.

    I got a problem in Qt using vector references.

    The code below runs fine as pure Visual Studio 2017 C++ console application.

    But the same code gives the C2440 error message if it is compiled in Qt Creator as C++ console application (without QT), as Qt console application or as simple QWidget application. The same happens if the code is compiled in Visual Studio as Qt application.

    //
    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<int> vec { 0,1,2,3 };
        for (auto& v = vec.begin(); v != vec.end(); v++)
        {
        }
    
        return 0;
    }
    

    The error message is:
    error C2440: "Initialisation": "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>" can't be converted to "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>> &"

    And in the code the first "vec" in the loop is marked red.

    Without using reference the code runs fine on all platforms.
    But I want to use reference because the code is in a performance critical part of my application.

    So why does the code run fine as Visual Studio C++ console application and doesn't compile in Qt? What's wrong?

    Regards,
    mireiner

    J 1 Reply Last reply 4 Dec 2018, 13:39
    0
    • C Offline
      C Offline
      CKurdu
      wrote on 4 Dec 2018, 13:36 last edited by
      #2

      Hi
      Do you have "CONFIG += c++11" in your pro file?

      You reap what you sow it

      J 1 Reply Last reply 4 Dec 2018, 13:40
      0
      • M mireiner
        4 Dec 2018, 13:34

        Hi there,

        I'm using Qt 5.9.7 with Visual Studio 2017 64bit compiler or alternative Visual Studio 2017 with Qt Viusal Studio Tools 2.2.2 programming Windows desktop application.

        I got a problem in Qt using vector references.

        The code below runs fine as pure Visual Studio 2017 C++ console application.

        But the same code gives the C2440 error message if it is compiled in Qt Creator as C++ console application (without QT), as Qt console application or as simple QWidget application. The same happens if the code is compiled in Visual Studio as Qt application.

        //
        #include <iostream>
        #include <vector>
        
        int main()
        {
            std::vector<int> vec { 0,1,2,3 };
            for (auto& v = vec.begin(); v != vec.end(); v++)
            {
            }
        
            return 0;
        }
        

        The error message is:
        error C2440: "Initialisation": "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>" can't be converted to "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>> &"

        And in the code the first "vec" in the loop is marked red.

        Without using reference the code runs fine on all platforms.
        But I want to use reference because the code is in a performance critical part of my application.

        So why does the code run fine as Visual Studio C++ console application and doesn't compile in Qt? What's wrong?

        Regards,
        mireiner

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 4 Dec 2018, 13:39 last edited by
        #3

        @mireiner Remove & after auto. begin() returns an iterator and not a reference to an iterator.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply 4 Dec 2018, 13:47
        1
        • M Offline
          M Offline
          mireiner
          wrote on 4 Dec 2018, 13:40 last edited by
          #4

          Yes I got
          CONFIG += console c++11
          and tried also
          CONFIG += console c++1z
          and
          QMAKE_CXXFLAGS += -std:c++latest
          all with no luck :-(

          1 Reply Last reply
          0
          • C CKurdu
            4 Dec 2018, 13:36

            Hi
            Do you have "CONFIG += c++11" in your pro file?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 4 Dec 2018, 13:40 last edited by
            #5

            @CKurdu As far as I know this is not needed with Microsoft compiler.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J jsulm
              4 Dec 2018, 13:39

              @mireiner Remove & after auto. begin() returns an iterator and not a reference to an iterator.

              M Offline
              M Offline
              mireiner
              wrote on 4 Dec 2018, 13:47 last edited by mireiner 12 Apr 2018, 13:52
              #6

              @jsulm

              Sorry I mainly used ranged based loops and not very often iterators. So let me ask again to make sure:

              You mean: Reference can't be used here?
              Is there any way to use iterators with reference at all? How does the code look like then?

              A 1 Reply Last reply 4 Dec 2018, 13:58
              0
              • M mireiner
                4 Dec 2018, 13:47

                @jsulm

                Sorry I mainly used ranged based loops and not very often iterators. So let me ask again to make sure:

                You mean: Reference can't be used here?
                Is there any way to use iterators with reference at all? How does the code look like then?

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 4 Dec 2018, 13:58 last edited by
                #7

                @mireiner an iterator is a reference (or a pointer, to be more specific)...

                Qt has to stay free or it will die.

                1 Reply Last reply
                1

                1/7

                4 Dec 2018, 13:34

                • Login

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