Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing a vector of pointers

Passing a vector of pointers

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
5 Posts 2 Posters 0 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
    mcoloney
    wrote on last edited by
    #1

    Friends, I have a vector of pointers: vector< PhoneRecord*> phonerecs(MAX); I want to pass this vector to a function so I can add things to it outside of main(). The function declartion is: void addPhoneRecord(PhoneRecord* records[]); and call it from main() using something like: addPhoneRecord(phonerecs); The error I'm getting is: error C2664: 'addPhoneRecord' : cannot convert parameter 1 from 'class std::vector<class PhoneRecord *,class std::allocator<class PhoneRecord *> >' to 'class PhoneRecord *[]' I understand I'm not passing this correctly... I'm sure this is a somewhat easy solution, but I can't seem to pinpoint it. Thanks in advance!

    D 1 Reply Last reply
    0
    • M mcoloney

      Friends, I have a vector of pointers: vector< PhoneRecord*> phonerecs(MAX); I want to pass this vector to a function so I can add things to it outside of main(). The function declartion is: void addPhoneRecord(PhoneRecord* records[]); and call it from main() using something like: addPhoneRecord(phonerecs); The error I'm getting is: error C2664: 'addPhoneRecord' : cannot convert parameter 1 from 'class std::vector<class PhoneRecord *,class std::allocator<class PhoneRecord *> >' to 'class PhoneRecord *[]' I understand I'm not passing this correctly... I'm sure this is a somewhat easy solution, but I can't seem to pinpoint it. Thanks in advance!

      D Offline
      D Offline
      Dave Bryant
      wrote on last edited by
      #2

      Try: addPhoneRecord( &phonerecs.front() ); Dave http://www.cloudsofheaven.org

      M 1 Reply Last reply
      0
      • D Dave Bryant

        Try: addPhoneRecord( &phonerecs.front() ); Dave http://www.cloudsofheaven.org

        M Offline
        M Offline
        mcoloney
        wrote on last edited by
        #3

        Dave, thanks again for your quick and informative reply. Allow me to include better code representation: My function definition is: void addPhoneRecord(string& name, string& address, string& phone, PhoneRecord* records[]) { Name* nameEntry = new Name(); nameEntry->set_name(name); Address* addressEntry = new Address(); addressEntry->set_address(address); PhoneRecord* phoneEntry = new PhoneRecord(nameEntry, addressEntry); phoneEntry->set_phoneNumber(phone); records.push_back(phoneEntry); } When I call it from main I'm using: addPhoneRecord(name, address, phone, &phonerecs.back()); All of the setting functions and the like work like they should, and properly. The error I'm getting is: error C2228: left of '.push_back' must have class/struct/union type If I comment out the records.push_back(phoneEntry); it compiles fine, but I get the standard WinXP error report problem. Any suggestions? Thanks again.

        D 1 Reply Last reply
        0
        • M mcoloney

          Dave, thanks again for your quick and informative reply. Allow me to include better code representation: My function definition is: void addPhoneRecord(string& name, string& address, string& phone, PhoneRecord* records[]) { Name* nameEntry = new Name(); nameEntry->set_name(name); Address* addressEntry = new Address(); addressEntry->set_address(address); PhoneRecord* phoneEntry = new PhoneRecord(nameEntry, addressEntry); phoneEntry->set_phoneNumber(phone); records.push_back(phoneEntry); } When I call it from main I'm using: addPhoneRecord(name, address, phone, &phonerecs.back()); All of the setting functions and the like work like they should, and properly. The error I'm getting is: error C2228: left of '.push_back' must have class/struct/union type If I comment out the records.push_back(phoneEntry); it compiles fine, but I get the standard WinXP error report problem. Any suggestions? Thanks again.

          D Offline
          D Offline
          Dave Bryant
          wrote on last edited by
          #4

          Since you are actually trying to use the vector as a vector (instead of just an array as you indicated in your previous message), you should pass a reference to the vector into the function. The last parameter should be: vector< PhoneRecord*>& records - then you will be able to use the push_back member. Dave http://www.cloudsofheaven.org

          M 1 Reply Last reply
          0
          • D Dave Bryant

            Since you are actually trying to use the vector as a vector (instead of just an array as you indicated in your previous message), you should pass a reference to the vector into the function. The last parameter should be: vector< PhoneRecord*>& records - then you will be able to use the push_back member. Dave http://www.cloudsofheaven.org

            M Offline
            M Offline
            mcoloney
            wrote on last edited by
            #5

            Dave, Thank you again. You're a master.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups