creating an array of objects in c++
- by tim22
I'm trying to create an array of objects in c++.
I'm creating a employee object, from my constructor in my company class here:
employee obj(int tempIdNum, double tempBase, double tempSales, double tempRate);
emp[tempcount]=obj; (this doesn't work?)
Emp is the name of the array which is defined here, located in my company h file.
Employee emp[4];
more code:
Company::Company(string name, string fileName){
string str;
int tempcount;
int tempIdnum;
double tempBase;
double tempSales;
double tempRate;
double num;
double arr[16];
this->name=name;
//Commission temp;
ifstream readFile;
readFile.open("fileName");
int inc=0;
while(tempcount<4){
for(int i=0+inc; i<4+inc; i++){
readFile>>num;
arr[i-inc]=num;
}
tempIdnum=(int)(arr[0]);
tempBase=arr[1];
tempSales=arr[2];
tempRate=arr[3];
Employee obj(int tempIdNum, double tempBase, double tempSales, double tempRate);
emp[tempcount]=obj;
inc+=4;
tempcount++;
}
readFile.close();
}
Here is some more from my h file
#include <string>
include "Commission.h"
using namespace std;
ifndef Company_H
define Company_H
class Company{
private:
string name; //name of company
Employee emp[4]; //array of payrool info about 4 commission employees
int numEmp; //number of employees
public:
Company();
Company(string name, string fileName);
~Company();
string getName();
Commission getEmployee(int element);
int getNumEmp();
};
endif
enter code here
Does not compile: 46 E:\exercise2\Company.cpp no match for 'operator=' in '((Company*)this)-Company::emp[tempcount] = obj'