Passing a vector of pointers
-
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! -
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!Try: addPhoneRecord( &phonerecs.front() ); Dave http://www.cloudsofheaven.org
-
Try: addPhoneRecord( &phonerecs.front() ); Dave http://www.cloudsofheaven.org
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 therecords.push_back(phoneEntry);
it compiles fine, but I get the standard WinXP error report problem. Any suggestions? Thanks again. -
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 therecords.push_back(phoneEntry);
it compiles fine, but I get the standard WinXP error report problem. Any suggestions? Thanks again.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
-
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