Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ scope problem

Name: vexed 2012-03-28 17:14

I am writing a program that has an employee struct and a company class. I have a function within my company class that adds an employee object to a list of employees. When I add the employee to the company, I want the data member "employer" inside my employee object to change.

struct employee{
public:
    ...
    string employer;
    string getemployer();
    void changeEmployer(string x);
    ...
private:
    ...
};

string employee::getemployer(){
    return employer;
}

void employee::changeEmployer(string x){
    this->employer = x;
}

class company{
public:
    company(string x){name = x;}
   
    list<employee> employees;

    string getname(){return name;};
    void addEmployee(employee newperson);
    void company::removeEmployee(employee quitter);
    void printemployees();
private:
    string name;
};

void company::addEmployee(employee newperson){
    list<employee>::iterator addemp = employees.begin();
    advance(addemp, 0);
    employees.insert(addemp, newperson);
    newperson.changeEmployer(this->getname());   
}

The last line that reads: "newperson.changeEmployer(this->getname());" appears to be ineffective because when I check to see if the employee's employer has been changed, it shows that it has NOT.

I'm assuming this is a scope problem. Is it possible to do this? Does anyone know how I can change the employer data member for the employee object?

In my main function I have the following:

int main(){
   
    employee Sally("Sally");
    company Google("Google");

    Google.addEmployee(Sally);

    cout << Sally.getemployer();


    return 0;
}

When I build and run, it doesn't print the new employer. It's as if the employer was not changed by adding the employee to the Google company.

Thanks in advance for any insight anyone is willing to share.

(p.s. the data types are specified in the assignment requirements so I can't change lists to vectors, or structs to classes, etc.)

Name: Anonymous 2012-03-29 1:02

>>4
you'd think so, but no, seeples actually went and did it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List