Thread: Passing custom classes as arguments

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Strings don't copy over

    Code:
    class CEmployee {
    string name; ...
    };
    
    CEmployee (const CEmployee &e ) {
    		int i;
    
    		this->name = e.name;
    ...
    	}
    
    ...
    
    void ProcessMorning(std::vector<CEmployee> &vEmps, int day) {
    	unsigned int i;
    	std::vector<CEmployee> openers;
    
    	for(i=0; i<vEmps.size(); i++) {
    		openers.push_back(vEmps[i]);
    	}
    }
    When I try pushing the copy of the copy of the CEmployee class into the vector, the name string doesn't copy over.
    Last edited by Sfpiano; 07-12-2005 at 10:27 PM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I'm not familiar with STL vectors, but you should overload operator = in addition to the copy constructor.

    PHP Code:
    #include <string>

    class CEmployee
    {
    private:
        
    std::string m_strName;

    public:
        
    CEmployee( const CEmployeeother 
        {
            *
    this other;
        }

        
    CEmployeeoperator = ( const CEmployeeother )
        {
            
    this->m_strName other.m_strName;

            return *
    this;
        }
    }; 
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Tried that didn't work. I really don't get it because it's just the string parameter that doesn't copy over, everything else does.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is nothing wrong with the posted code. The problem is elsewhere.

    If you have only strings and POD types (like int, bool or double) in your class, you don't need a copy constructor or copy assignment operator. It will work automatically.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    ...and if your openers vector is just supposed to be a copy of the vEmps vector, you should be able to forgo the for loop and just use the vector's own copy constructor.

    Code:
    void ProcessMorning(std::vector<CEmployee> &vEmps, int day) {
        std::vector<CEmployee> openers(vEmps);  // Do this instead of the 'for' loop
    
        ...
    
    }
    Also, technically if you wanted to use the for loop, your i variable should be of type vector<int>::size_type and not unsigned int.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I agree the problem is somewhere elsw amd I am going to take a guess that you are tying to use the vector openers outside of the ProcessMorning function which you can't because openers will be a local variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments to the system
    By ech0wave in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 11:15 AM
  2. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 10:04 PM
  3. prototypes for passing custom types
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-04-2002, 12:51 PM
  4. help Passing Arguments
    By cis in forum C Programming
    Replies: 3
    Last Post: 03-08-2002, 08:33 PM
  5. Replies: 2
    Last Post: 03-07-2002, 10:14 AM