C++ C++;虚空

C++ C++;虚空,c++,pointers,virtual,void,C++,Pointers,Virtual,Void,好的,我有一个名为employee的父类和3个名为manager、Research和engineer的子类。我做了一个向量,想把它们列出来。这就是我制作的过程 vector <Employee*,Manager*> EmployeeDB; Employee *temp; temp = new Manager(first, last, salary, meetings, vacations); EmployeeDB.push_back(temp); Employee.cpp: #i

好的,我有一个名为employee的父类和3个名为manager、Research和engineer的子类。我做了一个向量,想把它们列出来。这就是我制作的过程

vector <Employee*,Manager*> EmployeeDB;
Employee *temp;

temp = new Manager(first, last, salary, meetings, vacations);
EmployeeDB.push_back(temp);
Employee.cpp

#include <iostream>
#include <string>
using namespace std;

#ifndef EMPLOYEE_h
#define EMPLOYEE_h

class Employee
{
public:
    Employee();
    Employee(string firstname, string lastname, int salary);
    string getFname();
    string getLname();
    int getSalary();

    virtual void getInfo();

private:
    string mFirstName;
    string mLastName;
    int mSalary;

};
#endif
#include "Employee.h"

#include <iostream>
#include <string>
using namespace std;

Employee::Employee()
{
    mFirstName = "";
    mLastName = "";
    mSalary = 0;
}

Employee::Employee(string firstname, string lastname, int salary)
{
    mFirstName = firstname;
    mLastName = lastname;
    mSalary = salary;
}

string Employee::getFname()
{
    return mFirstName;
}

string Employee::getLname()
{
    return mLastName;
}

int Employee::getSalary()
{
    return mSalary;
}

void Employee::getInfo()
{
    cout << "Employee First Name: " << mFirstName << endl;
    cout << "Employee Last Name: " << mLastName << endl;
    cout << "Employee Salary: " << mSalary << endl;
}
#include <vector>
#include <iostream>
#include <string>

#include "Employee.h"
#include "Engineer.h"
#include "Manager.h"
#include "Researcher.h"
using namespace std;

vector <Employee*> EmployeeDB;
Employee *temp;

void add()
{
    int emp, salary, vacations, meetings, exp, c;
    string first, last, type, school, topic;
    bool skills;

    do
    {
        system("cls");
        cout << "===========================================" << endl;
        cout << "               Add Employee                " << endl;
        cout << "===========================================" << endl;
        cout << "[1] Manager." << endl;
        cout << "[2] Engineer." << endl;
        cout << "[3] Researcher." << endl;
        cout << "Input choice: ";
        cin >> emp;
        system("cls");
    } while (emp <= 0 || emp > 3);

    cout << "===========================================" << endl;
    cout << "              Employee  Info               " << endl;
    cout << "===========================================" << endl;
    cout << "Employee First name: ";
    cin >> first;
    cout << "Employee Last name: ";
    cin >> last;
    cout << "Employee Salary: ";
    cin >> salary;

    switch (emp)
    {
    case 1:
        cout << "Employee numbers of meetings: ";
        cin >> meetings;
        cout << "Employee numbers of vacations: ";
        cin >> vacations;

        temp = new Manager(first, last, salary, meetings,vacations);
        EmployeeDB.push_back(temp);
        delete temp;

        break;
    case 2:
        cout << endl;
        cout << "[1]YES    [2]NO" << endl;
        cout << "Employee C++ Skills: ";
        cin >> c;
        if (c == 1)
        {
            skills = true;
        }
        else
        {
            skills = false;
        }

        cout << "Employee Years of exp: ";
        cin >> exp;
        cout << "(e.g., Mechanical, Electric, Software.)" << endl;
        cout << "Employee Engineer type: ";
        cin >> type;

        temp = new Engineer(first, last, salary, skills, exp, type);
        EmployeeDB.push_back(temp);
        delete temp;
        break;
    case 3:
        cout << "Employee School where he/she got his/her PhD: ";
        cin >> school;
        cout << "Employee Thesis Topic: ";
        cin >> topic;

        temp = new Researcher(first, last, salary, school, topic);
        EmployeeDB.push_back(temp);
        delete temp;
        break;
    }
}

void del()
{

}

void view()
{
    for (int x = 0; x < (EmployeeDB.size()); x++)
    {
        cout << EmployeeDB[x]->getInfo();
    }
}

void startup()
{

    cout << "===========================================" << endl;
    cout << "             Employee Database             " << endl;
    cout << "===========================================" << endl;
    cout << "[1] Add Employee." << endl;
    cout << "[2] Delete Employee." << endl;
    cout << "[3] List Employees." << endl;
    cout << "[4] Exit." << endl;
    cout << "Please Enter Your Choice: ";
}

int main(int argc, char** argv)
{
    bool flag = true;
    int choice;

    do {
        do 
        {
            system("cls");
            system("pause>nul");
            startup();
            cin >> choice;
        } while (choice < 0 || choice >4);

        switch (choice)
        {
        case 1:
            add();
            break;
        case 2:
            del();
            break;
        case 3:
            view();
            break;
        case 4:
            flag = false;
            system("EXIT");
            break;
        }
    } while (flag == true);

    return 0;
    system("pause>nul");
}
#包括“Employee.h”
#包括
#包括
使用名称空间std;
雇员::雇员()
{
mFirstName=“”;
mLastName=“”;
mSalary=0;
}
Employee::Employee(字符串firstname、字符串lastname、整数工资)
{
mFirstName=firstname;
mLastName=lastname;
澳门特别行政区=工资;
}
字符串Employee::getFname()
{
返回mFirstName;
}
字符串Employee::getLname()
{
返回mLastName;
}
int Employee::getSalary()
{
返回澳门特别行政区;
}
void Employee::getInfo()
{

cout问题在于
getInfo
具有返回类型
void
,您正试图将该返回值放入cout


重要的是要理解代码
std::cout您应该在向量
EmployeeDB
中存储什么?是
Manager
Employee
派生出来的?然后只使用
Employee*
作为向量中的类型。
getInfo
不会返回任何内容。尝试不向任何对象传递任何内容有什么意义
cout
?要么实现
运算符,要么因为
getInfo
不返回任何内容。但这不是代码中最大的问题,您正在用悬空指针填充
EmployeeDB
,因为您立即删除刚插入的指针指向的对象
EmployeeDB。向后推(temp);delete temp;
。如果要保留指针,则1)使析构函数为虚拟的2)存储
唯一的\u ptr
而不是原始指针3)为它们分配
使唯一的
不删除任何手动新建/删除我使用了void,因为我只想在某个向量内计算值,例如myvector[1]我想知道这件事的头、尾和薪水,我该怎么办?
for (...)
{
  EmployeeDB[x]->getInfo();
}
std::string getInfo()
{
  std::string info;
  info =...
  return info;
}
view()
{
  for (...)
  {
    std::cout << EmployeeDB[x];
  }
}