簡體   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