-
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.
-
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 CEmployee& other )
{
*this = other;
}
CEmployee& operator = ( const CEmployee& other )
{
this->m_strName = other.m_strName;
return *this;
}
};
-
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.
-
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.
-
...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.
-
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.