creating an array of objects in c++
Posted
by
tim22
on Stack Overflow
See other posts from Stack Overflow
or by tim22
Published on 2011-01-06T07:16:43Z
Indexed on
2011/01/06
7:53 UTC
Read the original article
Hit count: 230
c++
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'
© Stack Overflow or respective owner