C++ 虚拟虚空

C++ Virtual Void

好的,所以我有一个父 class 称为员工,3 个子class 称为经理、研究员和工程师。我制作了一个矢量并想列出它们。这就是我处理制作的方式。

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

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

我制作矢量没问题,我关心的是列出信息。所有 3 个 subclasses 都有 firstnamelastnamesalary,但它们的区别在于它们具有不同的数据成员,这是唯一的,例如 Manager具有 intvacation 并且 Engineer 具有 intexperience 等等。

Employee.h:

#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

Employee.cpp:

#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");
}

我在 view() 函数上遇到错误。

它说没有 operator<< 匹配这些操作数 二进制“<<”:未找到接受类型为 void 等的右手操作数的运算符

问题是 getInfo 有 return 类型 void 而您正试图将 return 值放入 cout。

了解代码 std::cout << val 实际上调用函数 operator<<(ostream& out, const objectType& val) 很重要,其中 objectType 是 'val' 的类型。

在您的情况下,类型为 void,并且 operator<< 根本没有将 void 作为类型的实现。因此错误 "no operator found which takes a right hand operand of type void...".

为了解决您的问题,您有以下几种选择:

  1. view()改成

    for (...)
    {
      EmployeeDB[x]->getInfo();
    }
    
  2. getInfo() 更改为 return 您想要的字符串信息:

    std::string getInfo()
    {
      std::string info;
      info =...
      return info;
    }
    
  3. 为 Employee 创建一个 operator<< 并更改视图以调用它:

    view()
    {
      for (...)
      {
        std::cout << EmployeeDB[x];
      }
    }