Thread: passing a vector as an arguement

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    passing a vector as an arguement

    how would i pass the member vector eEntries into the Add,Edit, and Delete... member functions?
    what i want to do is make a vector of phonebook entries... and then be able to add an entry onto the vector...delete an entry from anywhere in the vector... or edit any entry in the vector...

    Code:
    //// Structures ////
    class Entry
    {
          public:
                 void Add();
                 void Delete();
                 void Edit();
                 void Display(fstream *fFile);
                 void Write(fstream *fFile);
                 
          protected:
                 int ordernum;
                 string name;
                 string cnumber;
                 string hnumber;
                 string address;
                 vector<Entry> eEntries;
    };

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would do th like this

    Code:
    struct Entry {
                 int ordernum;
                 string name;
                 string cnumber;
                 string hnumber;
                 string address;
    };
    
    class Phonebook
    {
          public:
                 void Add();
                 void Delete();
                 void Edit();
                 void Display(fstream *fFile);
                 void Write(fstream *fFile);
        private:             
                 vector<Entry> eEntries;
    };
    Kurt
    Last edited by ZuK; 05-13-2006 at 03:09 PM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If it's a member function, then why would you pass it as a parameter? From all the public functions there you can access eEntries as the vector is part of the class. For non member functions, you'd have to give yourself some way of accessing the vector from outside of the class with some kind of getter function. When you do that, you would do
    Code:
    void Add(vector<Entry>& eEntries);
    and the call...
    Code:
    Add(myEntryObj.getEntries());
    Edit, also Zuk's suggestion of breaking down the class is correct. Not many things have a vector of itself in itself. Think of a "has a" relationship between the Entry and what's storing the entries.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    right right.... so for the code... i could put
    Code:
    struct Entry {
                 int ordernum;
                 string name;
                 string cnumber;
                 string hnumber;
                 string address;
    };
    
    class Phonebook
    {
          public:
                 void Add();
                 void Delete();
                 void Edit();
                 void Display(fstream *fFile);
                 void Write(fstream *fFile);
        private:
                 Entry eEntry;             
                 vector<Entry> vEntries;
    };
    
    ...
    ...
    ...
    void Phonebook::Add()
    {
         cout<<"\nName: ";
         cin<<eEntry.name;
         cout<<"\nCell Phone Number: ";
         cin<<eEntry.cnumber;
         (so on and so forth)
         vEntries.push(eEntry);
    }
    is this how i would be able to access the private vector eEntries?

    And also, is the a proper way to define a class function? Or should it be done in the class definition as well?

    Code:
    class Phonebook
    {
          public:
    
                 Phonebook(){}
                 ~Phonebook(){}
    
                 void Add()
                 {
                       cout<<"\nName: ";
                       cin<<eEntry.name;
                       cout<<"\nCell Phone Number: ";
                       cin<<eEntry.cnumber;
                       (so on and so forth)
                       vEntries.push(eEntry);
                 }
    
                 void Delete();
                 void Edit();
                 void Display(fstream *fFile);
                 void Write(fstream *fFile);
        private:
                 Entry eEntry;
                 vector<Entry> vEntries;
    };

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It looks better. I wouldn't make eEntry a member, though. It would be better as a temp variable in the function.

    There are logic reasons to define a method inside or outside of a class depending on the situations. Mainly, the reasons you define outside of a class and more likely in a separate source file is because you don't want to crowd the class so it more easily read and also it's good to leave a class open to more than one implementation. Small functions that do obvious tasks like getters or setters could be defined inside of the class or if your class has one distinct purpose you could leave the whole implementation in the definition. You'll find the standard libraries are defined that way.

    Also, as personal preference, I would make Entry a class with private members and make it a friend class of Phonebook. It may make little difference in your situation, though.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    soo here is another attempt

    phonebook.h
    Code:
    void Phonebook::Add()
    {
         Entry tempEntry;
         
         cout<<"\nName: ";
         cin<<eEntry.name;
         cout<<"\nCell Phone Number: ";
         cin<<eEntry.cnumber;
         (so on and so forth)
         vEntries.push(eEntry);
    }
    
    void Phonebook::Delete()
    {
         int delete;
    
         cout<<"\nDelete which entry? ( 1 - "<<eEntries.size() - 1<<" )";
         cout<<"\n->: ";
         cin>>delete;
         eEntries.erase(delete-1);
    }
    
    void Phonebook::Edit()
    {
         int edit;
    
         cout<<"\nEdit which entry? ( 1 - "<<eEntries.size() - 1<<" )";
         cout<<"\n->: ";
         cin>>edit;
         
         eEntries[edit-1].name.clear();
         eEntries[edit-1].cnumber.clear();
         eEntries[edit-1].hnumber.clear();
         eEntries[edit-1].address.clear();
    
         cout<<"\n\nName: ";
         cin>>eEntries[edit-1].name;
         cout<<"\n\nCell Phone Number: ";
         cin>>eEntries[edit-1].cnumber;
         cout<<"\n\nHome Phone Number: ";
         cin>>eEntries[edit-1].hnumber;
         cout<<"\n\nAddress: ";
         cin>>eEntries[edit-1].address;
    }
    phonebook.cpp
    Code:
    struct Entry
    {
                 int ordernum;
                 string name;
                 string cnumber;
                 string hnumber;
                 string address;
    };
    
    class Phonebook
    {
          public:
                 void Add();
                 void Delete();
                 void Edit();
                 void Display(fstream *fFile);
                 void Write(fstream *fFile);
        private:           
                 vector<Entry> vEntries;
    };
    Ok... so ive added some more details to what i want to be happening.... i think i have gotten it...

    Also... would there be any place in the code where a pointer would be sufficient?
    I am wanting to try to use pointer more often ... than wasting memory and code..

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I see no place in your code suitable for a pointer. Anyway, onto your code.

    First off... you're ass backwards with the definition and implementation.

    The implementation is supposed to go in a .cpp while the definition goes in the header. You also have to fix your identifier names in your Add() method. You're declaring a tempEntry and assigning to an eEntry. Also, some slightly better implementation for your Edit() would be nice. Better that you let people edit single parameters of an entry rather than clearing the whole thing and making them reenter it all. You might as well just delete and add again.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    First off... you're ass backwards with the definition and implementation.
    lol @^this^
    ha ok ok... i guess i got confused when you said i need to define in another source file...

    I AM NOT A COMPILER. PLEASE DO NOT TREAT ME LIKE ONE.
    vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvv
    You also have to fix your identifier names in your Add() method. You're declaring a tempEntry and assigning to an eEntry.
    lol...i just thought that was funny... but thats for catching my syntax error...

    As for the editing: I thought about that after I posted...i will make a switch statement in a while loop for the user response on what section he wants to edit...this would allow the user to edit whatever he wants until done=true..

    example...
    Code:
    void Phonebook::Edit()
    {
         int edit;
         int field;
         bool done=false;
    
         cout<<"\nEdit which entry? ( 1 - "<<eEntries.size() - 1<<" )";
         cout<<"\n->: ";
         cin>>edit;
    
         // DISPLAY 1 ENTRY
    
       while(done==false)
       {
         cout<<"\n\n<>Which field to edit?";
         cout<<"\n\t<1>Name";
         cout<<"\n\t<2>Cell Phone Number";
         cout<<"\n\t<3>Home Phone Number";
         cout<<"\n\t<4>Address";
         cout<<"\n\t<5>Done";
         cout<<"\n->: ";
         cin>>field;
         switch(field)
         {
              case 1:
                        eEntries[edit-1].name.clear();
                        cout<<"\n\nName: ";
                        cin>>eEntries[edit-1].name;
                        break;
              case 2:
                        eEntries[edit-1].cnumber.clear();
                        cout<<"\n\nCell Phone Number: ";
                        cin>>eEntries[edit-1].cnumber;
                        break;
              case 3:
                        eEntries[edit-1].hnumber.clear();
                        cout<<"\n\nHome Phone Number: ";
                        cin>>eEntries[edit-1].hnumber;
                        break;
              case 4:
                        eEntries[edit-1].address.clear();
                        cout<<"\n\nAddress: ";
                        cin>>eEntries[edit-1].address;
                        break;
              case 5:
                        done=true;
                        break;
              defualt:
                        cout<<"\nNot a valid choice!";
        }
      }
    
          // CHECK IF USER IS OK WITH EDITED ENTRY
    
    }
    Last edited by gL_nEwB; 05-14-2006 at 11:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct vs. reference
    By reakinator in forum C Programming
    Replies: 4
    Last Post: 06-14-2008, 10:11 PM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM