I am trying to store either the objects of a base class ( employee
), or pointers to the objects inside a vector/array in another class ( finance
) object. The number of employee objects depends on the user, so it needs to work dynamically. So far I have this:
finance.h
#ifndef FINANCE
#define FINANCE
#include "freight.h"
class finance
{
public:
finance();
~finance();
};
#endif // FINANCE
finance.cpp
#include "finance.h"
using namespace std;
finance::finance()
{
vector<employee *> vemployee; //first problem line
}
finance::~finance()
{
}
main.cpp
void add_manager()
{
string name;
name = get_string_input("Please input the name of the employee.");
vManagers.push_back(new manager(name)); //second problem line
ask_employee();
}
Main.cpp
also has includes on all my .h
files as well as finance.cpp
. I am getting errors on both main and finance.cpp
saying about expected primary expressions and not declared in scope.
I'm clearly doing something wrong but I honestly have no idea as vectors is something I haven't been taught yet. If there's a way to do it with arrays I don't mind trying that either.
Ok, you need to keep vManagers in class declaration:
//finance.h file
#ifndef FINANCE
#define FINANCE
#include "freight.h" //assuming you have defined manager class here
class finance
{
public:
finance();
~finance();
void add_manager();
private:
vector<manager*> vManagers;
};
#endif // FINANCE
//finance.cpp file
#include "finance.h"
using namespace std;
finance::finance()
{
}
finance::~finance()
{
for(int i=0; i< vManagers.size(); i++)
{
if(vManagers[i] != NULL)
{
delete vManagers[i];
}
}
}
finance::add_manager()
{
string name;
name = get_string_input("Please input the name of the employee.");
vManagers.push_back(new manager(name)); //second problem line
while(ask_employee()
{
name = get_string_input("Please input the name of the employee.");
vManagers.push_back(new manager(name)); //second problem line
}
}
now you can create and use finance object in main.cpp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.