打印方法不返回任何内容 [英] Print method not returning anything

查看:86
本文介绍了打印方法不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类Employee,它有两个子类Manager和Salesperson。在Employee类中,我有一个名为 addSalesperon(Salesperson *)的方法和一个向量成员变量 vector< Salesperson *> staff_list 存储员工对象的位置。 addSalesperon(Salesperson *)只需将销售人员推送到向量中即可。每个员工还有另一个向量向量< Insurance *> sale_list; 存储来自我创建的名为Insurance的类的Class对象。



我有一个print方法循环遍历 staff_list 向量中的Employee对象并打印存储的叛乱对象在 sales_list 在staff_list的每个索引处的向量。 (对不起,我可能解释得很差但是如果你看一下代码,你应该能够轻松看到我在解释的内容)



问题是当我试着打电话的时候打印方法没有打印,但如果我自己打印 staff_list sale_list 矢量,则打印正常。我只是不确定我的打印方法有什么问题,并且非常感谢帮助清理它!



样本主要:

I have a base class "Employee" that has two child classes "Manager" and "Salesperson". In the Employee class I have a method called addSalesperon( Salesperson * ) and a vector member variable vector<Salesperson *> staff_list where it stores employee objects. addSalesperon( Salesperson * ) just pushes the salesperson into the vector. Each employee also has another vector vector<Insurance *> sale_list; that stores Class objects from a class I create called "Insurance".

I have a print method that loops through the Employee objects in staff_list vector and prints the the insurgence objects stored in sales_list vector at each index of staff_list. (Sorry I probably explained that poorly but if you look at the code you should be able to easily see what I was explaining)

The problem is when I try to call the print method nothing gets printed, but if I print the staff_list and sale_list vectors on their own they print fine. I'm just not sure whats wrong with my print method, and help clearing this up will be greatly appreciated!

Sample main:

list<Insurance *> sales_list; // List of all sales made by any employee
vector<Employee *> employee_list; // List of all existing employees

Employee *m = new Manager("Bob", "Smith", 62000);
Employee *s1 = new Manager("Rick", "Smith", 45000);
Employee *s2 = new Manager("Dick", "Smith", 25000);
Insurance *a = new Auto("John", "Smith", "ford", "mustang", 123456789, 600, 796);
Insurance *b = new Home("Larry", "Smith", 45, 670, 3220, 4120);

    
m->addSalesperson(s1);
m->addSalesperson(s2);
s1->addSale(a);
s2->addSale(b);

employee_list.push_back(m);
employee_list.push_back(s1);
employee_list.push_back(s2);
sales_list.push_back(a);
sales_list.push_back(b);

printStaffSales::operator<<(cout, *m);





员工类:



Employee class:

class Employee{
protected:
    string first_name;
    string last_name;
    string employee_type;
    float base_salary;
    float commission;
    float total_salary;
    vector<Employee *> staff_list;
public:
    vector<Insurance *> sale_list; // holders pointers to the abstract Insurance class objects
    Employee():first_name(""), last_name(""), base_salary(0){ };
    Employee(string fn, string ln, float bs):first_name(fn), last_name(ln), base_salary(bs){ };

    void addSale( Insurance * );
    void addSalesperson( Employee * );

    virtual ostream &print_staff_sales( ostream & ) = 0;
    //Other methods left out
};

//2 other namespaces for different overloaded output operators not shown (they Work fine)
namespace printStaffSales {
    ostream &operator<<( ostream &, Employee & );
}

Employee.cpp

ostream &printStaffSales::operator<<( ostream &strm, Employee &e ){
    // Virtual print
    cout<< "Sales by employees under manager: " << endl;
    return e.print_staff_sales( strm );
}





经理课程:



Manager Class:

class Manager: public Employee{
private:
    float staff_commission;
public:
    // Constructors
    Manager(): Employee(){ employee_type = "Manager"; };
    Manager(string fn, string ln, float bs): Employee(fn, ln, bs){ employee_type = "Manager";

    
    virtual ostream &print_staff_sales( ostream & );
    //Other methods left out
};





打印方式



Print method

ostream &Manager::print_staff_sales( ostream &strm ){
    strm << "Manager: " << first_name << " " << last_name << endl;
    
    Salesperson *s_temp;
    for (int i = 0; i < staff_list.size(); ++i){
        s_temp = staff_list.at(i);
        
        Insurance *i_temp;
        for (int i = 0; i < s_temp->sale_list.size(); ++i){
            i_temp = sale_list.at(i);
            strm<< *i_temp << endl;
        }
    }
    
    strm<< endl;
    return strm;
}

推荐答案

更改

Change
i_temp = sale_list.at(i);



to


to

i_temp = s_temp->sale_list.at(i);


你错过了OOP的全部想法。此函数存在于运行时类型 管理器的对象中,但您使用编译时引用此类的对象相同对象的类型,其中未定义此函数。



怎么办?许多人建议使用动态转换来预期运行时类型。它会起作用,但这根本不是解决方案,而是纯粹违反OOP。由于你没有以OOP方式做任何事情,所以在紧要关头 ad hoc 中它甚至不是一个可接受的解决方案。各种向下铸造会被滥用。



要告诉你想要做什么,你需要了解所有关于你的信息。要求。从本质上讲,您确实需要重新设计代码。为此,您需要基于虚拟函数学习所有OOP,尤其是后期绑定多态。主要思想是通过一些编译时类型的基类型访问一组对象,通过虚拟调度机制访问派生类型的所有功能。



关于这些主题和OOP的文献很多。首先,你需要完全理解它。



-SA
You missed the whole idea of OOP. This function exists in the object of the runtime type Manager, but you reference the object of this class with the compile-time type of the same object, where this function is not defined.

What to do? Many would advise using dynamic cast to the prospective runtime type. It would work in a pinch, but this would not be a solution at all, but the pure violation of the OOP. As you are not doing anything in OOP way, it would not be even an acceptable solution in a pinch, ad hoc. All kinds of down-casting would be such abuse.

To tell you want exactly to do, one would need to know all about your requirements. Essentially, you really need to redesign your code. For this purpose, you need to learn all of OOP, especially late binding and polymorphism, both based on virtual function. The main idea is to have a set of objects accessible by some compile-time type of the base type, accessing all the features of derived types through virtual dispatch mechanism.

There is plenty of literature on these topics and OOP in general. To start with, you need to understand it all really well.

—SA


这篇关于打印方法不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆