hello,
i need help to solve this little problem.
i have an error here.."cant access privet members declared in class CWorker..with is another class

void main()
{
	CManager *Sasson = new CManager("Sasson Sassoni");
	assert(Sasson);
	// adds workers
	Sasson->addWorker("Jojo");
	Sasson->addWorker("Shilgiya", 1000);
}

i also have CManager.h...
in CManager.cpp, its only part very i got the problem.

void CManager::addWorker(const char *name,int salary)
{
	int i;
	for(i=0;i<m_numOfWorkers;i++);				
	CWorker* p=m_pWorker;
	m_pWorker=new worker[i+2];					
	assert(m_pWorker);
	memcpy(m_pWorker,p,(i+1)*sizeof(worker));		
	delete []p;		
	worker.m_wName=name;
	worker.m_wSalary=salary;
	m_pWorker[i]=worker;
	

}
class CWorker
{
public:
	// default constructor
	CWorker(const char*,int salary=0);
	CWorker();
private:
	char m_wName[LEN_W];	
	int m_wSalary;
};
#endif
class CManager
{
	int m_mSalary;
	char m_mName[LEN_M];
	CWorker *m_pWorker;			// pointer to list of workers
	int m_numOfWorkers;			// number of workers subject to this manager
public:
	  // constructor
CManager(const char* , int salary=60000,int num=0);
	 //defult constructor
CManager();	
	// copy constructor
CManager(const CManager&);	
	// destructor
~CManager();	
	// adds one new worker at the end of the list
void 	addWorker(const char*,int salary=0,CWorker&);
	// find worker by name
//CWorker findWorker(const char* );
	// show manager details including list of workers*/
void showManager()const;
	// delete a worker from the list
/*deleteWorker(???)
	// updating the manager salary
void setMangerSalary(int);
	// updating the manager name
void setManagerName(const char* name);
		// retrieves the current manager
const CManager&getCurrentManager()const;	*/
};
#endif

if you could help me please ,
thank you,
michael

The error message says it all:
Since m_wName and m_wSalary are private to CWorker you cannot access them from within CManager.

yeha i know this , but i sill have to add a new Worker to the arry of workers under the manager..,
i don't understand how i do it..

what should i do to access the other class in Manager class, so that the main will look like this:

1.
      void main()
   2.
      {
   3.
      CManager *Sasson = new CManager("Sasson Sassoni");
   4.
      assert(Sasson);
   5.
      // adds workers
   6.
      Sasson->addWorker("Jojo");
   7.
      Sasson->addWorker("Shilgiya", 1000);
   8.
      }

You could for example just use CWorker's contructor: CWorker::CWorker(const char*,int salary=0); That's what it's for.

Like:

// in CManager::addWorker
CWorker *pworker = new CWorker(name, salary);
// add pworker to list

i'm sorry, i still don't get it.
how i change the function:

void CManager::addWorker(const char *name,int salary)
{
	int i;
	for(i=0;i<m_numOfWorkers;i++)	
	{
	CWorker *p=m_pWorker;
	m_pWorker=new CWorker();	
	}
	m_numOfWorkers++;
	assert(m_pWorker);
	memcpy(m_pWorker,p,(i+1)*sizeof(CWorker));		
	delete []p;
	m_pWorker[i].m_wSalary=salary;
	m_pWorker[i].m_wName=name;

}

Aah!

I think you are trying to copy and enlarge an array of CWorkers.

Well... :icon_eek:

If m_pWorker is supposed to be an array then I would strongly suggest you use a std::vector.

you kidding me??,,anyway that's what i have...
but when i am tring to do another function of print,it shows garbage.
i dont understand.

void CManager::addWorker(const char *name,int salary)
{
	int i;
	
	for(i=0;i<m_numOfWorkers;i++);
	CWorker *p=new CWorker(name,salary);
	m_pWorker=new CWorker[i+2];	
	m_numOfWorkers++;
	assert(m_pWorker);
	memcpy(m_pWorker,p,(i+1)*sizeof(CWorker));	
	m_pWorker[i]=*p;
	delete []p;
	
}
void CManager::showManager()const
{
	int i;
	cout<<m_mName<<", "<<m_mSalary<<"."<<" List of workers:";
	cout<<endl;
	for(i=0;i<m_numOfWorkers;i++)
				m_pWorker[i].showWorker();
	cout<<endl;	
}

showWorker is in different class..

void CWorker::showWorker()const
{
	cout<<" ("<<m_wName<<","<<m_wSalary<<") ";
	
}

you kidding me??

No.

,,anyway that's what i have... but when i am tring to do another function of print,it shows garbage.
i dont understand.

What does "print" have to do with CManager::addWorker()?

void CManager::addWorker(const char *name,int salary)
{
	int i;
	
	for(i=0;i<m_numOfWorkers;i++);
	CWorker *p=new CWorker(name,salary);
	m_pWorker=new CWorker[i+2];	
	m_numOfWorkers++;
	assert(m_pWorker);
	memcpy(m_pWorker,p,(i+1)*sizeof(CWorker));	
	m_pWorker[i]=*p;
	delete []p;
	
}

???

void CManager::showManager()const
{
	int i;
	cout<<m_mName<<", "<<m_mSalary<<"."<<" List of workers:";
	cout<<endl;
	for(i=0;i<m_numOfWorkers;i++)
				m_pWorker[i].showWorker();
	cout<<endl;	
}

What does showManager() have to do with addManager()?

showWorker is in different class..

void CWorker::showWorker()const
{
	cout<<" ("<<m_wName<<","<<m_wSalary<<") ";
	
}

What does showWorker() have to do with addManager()?

Did you even take a look at std::vector?

Or...

Are you kidding me? :icon_neutral:

Edit: Or maybe you just didn't read my las post?

Edited 5 Years Ago by Caligulaminus: n/a

In your CManager class, you should have a data member like:

std::vector<CWorker> m_Workers;

In your CWorker, as suggested, you should have a constructor like:

class CWorker {
  //...
  public:
    CWorker(const char* aName,int aSalary = 0) {
      m_wName = aName;
      m_wSalary = aSalary;
    };

And, your addWorker function becomes:

void CManager::addWorker(const char *name,int salary)
{
  m_Workers.push_back(CWorker(name,salary));
}

And that's it. std::vector is much better and easier to use because it takes care of increasing and decreasing the capacity of your array such that you can add/remove elements very easily (and efficiently).

i can't do it with str::vector...good idea but i must not..
that's how i should do it...

i did this one

CManager::CManager(const CManager&manager):m_pWorker(manager.m_pWorker)
{
	int i;
	for(i=0;i<m_numOfWorkers;i++);		
		m_pWorker=new CWorker[i+1];							
	assert(m_pWorker);
	memcpy(m_pWorker,manager.m_pWorker,(i+1)*sizeof(CWorker));				
	m_mSalary=manager.m_mSalary;					
	strcpy(m_mName,manager.m_mName);	 
}
CManager::~CManager()
{
	delete [] m_pWorker;							
}
void CManager::addWorker(const char *name,int salary)
{
	int i;
	
	for(i=0;i<m_numOfWorkers;i++);
	CWorker *p=new CWorker(name,salary);
	m_pWorker=new CWorker[i+2];
	m_numOfWorkers++;
	assert(m_pWorker);
	memcpy(m_pWorker,p,(i+1)*sizeof(CWorker));	
		m_pWorker[i]=*p;
	m_pWorker[i].showWorker();
	delete []p;
	

}

but when i try to print it not in the function for example do another function for print...it prints bullshit..its like there is nothing in *m_pWorker..

How about creating setName, setSalary, getName, getSalary in your CWorker class?

This article has been dead for over six months. Start a new discussion instead.