繁体   English   中英

C ++虚拟虚空

[英]C++ Virtual Void

好吧,所以我有一个称为雇员的父类和一个称为经理,研究人员和工程师的3个子类。 我做了一个向量,想列出它们。 这就是我制作过程的方式。

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

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

我制作矢量没有问题,我的担心是列出信息。 这3个子类都具有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返回类型为void并且您试图将返回值放入cout中。

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

在您的情况下,该类型为void,并且根本没有实现将void作为类型的operator<<实现。 因此,出现错误“找不到运算符,该运算符采用void类型的右手操作数”。

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

  1. view()更改为

     for (...) { EmployeeDB[x]->getInfo(); } 
  2. 更改getInfo()以根据需要返回字符串信息:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM