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. Simple question about QVector append for pointers
Forum Updated to NodeBB v4.3 + New Features

Simple question about QVector append for pointers

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 2.9k Views
  • 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.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by
    #1

    Hi,
    I have a QVector and I want to append elements into it. What is the correct way? I get always compiler errors.
    I dont want to post errors, I need only correct syntax :)

    .h file:

    private:
        QVector<SystemUnit> SystemUnits; 
        int AddElement(SystemUnit *SU);
        int RemoveElement(int index);
    

    .cpp file:

    
    int Facility::AddElement(SystemUnit *SU)
    {
        SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors.
        return 0;
    }
    

    How to append in a QVector from a pointer object?

    Thanks.

    jsulmJ VRoninV 2 Replies Last reply
    0
    • kahlenbergK kahlenberg

      Hi,
      I have a QVector and I want to append elements into it. What is the correct way? I get always compiler errors.
      I dont want to post errors, I need only correct syntax :)

      .h file:

      private:
          QVector<SystemUnit> SystemUnits; 
          int AddElement(SystemUnit *SU);
          int RemoveElement(int index);
      

      .cpp file:

      
      int Facility::AddElement(SystemUnit *SU)
      {
          SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors.
          return 0;
      }
      

      How to append in a QVector from a pointer object?

      Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @kahlenberg Your code is wrong:

      QVector<SystemUnit> SystemUnits;
      

      SystemUnits is a vector for SystemUnit not pointers to SystemUnit. It should be:

      QVector<SystemUnit*> SystemUnits;
      
      int Facility::AddElement(SystemUnit *SU)
      {
          // Since SU is already a pointer you do not need &, just fix SystemUnit like described above
          SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors.
          return 0;
      }
      

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

      1 Reply Last reply
      2
      • kahlenbergK kahlenberg

        Hi,
        I have a QVector and I want to append elements into it. What is the correct way? I get always compiler errors.
        I dont want to post errors, I need only correct syntax :)

        .h file:

        private:
            QVector<SystemUnit> SystemUnits; 
            int AddElement(SystemUnit *SU);
            int RemoveElement(int index);
        

        .cpp file:

        
        int Facility::AddElement(SystemUnit *SU)
        {
            SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors.
            return 0;
        }
        

        How to append in a QVector from a pointer object?

        Thanks.

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

        @kahlenberg said:

            SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors.
        

        The dereferencing operator in C++ is * not &

        Depends what you want to do here:

        • to store a copy of SU (not SU itself) use SystemUnits.append(*SU);
        • to store the same item SU is pointing to then declare QVector<SystemUnit*> SystemUnits; and use SystemUnits.append(SU);

        "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