Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 我能';“我不知道我的”怎么了;副本;变量_Oop_Copy Constructor - Fatal编程技术网

Oop 我能';“我不知道我的”怎么了;副本;变量

Oop 我能';“我不知道我的”怎么了;副本;变量,oop,copy-constructor,Oop,Copy Constructor,我不知道我的“copies”变量有什么问题 这是我家庭作业的演示代码,向我们展示了基本语法以及如何使用不同类型的OOP构造函数和关键字。问题是什么时候运行这一切都正常,但问题在于copies变量。当复制构造函数时,它的值应该是1,但它似乎不起作用 #include <iostream> #include <string> using namespace std; const int JANE_HOURS = 30, JIM_HOURS = 20, SETTER_HOURS

我不知道我的“copies”变量有什么问题

这是我家庭作业的演示代码,向我们展示了基本语法以及如何使用不同类型的OOP构造函数和关键字。问题是什么时候运行这一切都正常,但问题在于copies变量。当复制构造函数时,它的值应该是1,但它似乎不起作用

#include <iostream>
#include <string>
using namespace std;
const int JANE_HOURS = 30, JIM_HOURS = 20, SETTER_HOURS = 40;
const double SETTER_DAYS = 3.0, HOURS_PER_DAY = 24.0;
class WorkerHours {
    private:
        int hoursWorked;
        int copies;
    public:
        void setData(int);
        void setData(double);
        int getCpy();
        WorkerHours();
        WorkerHours(int);
        WorkerHours operator + (const WorkerHours &combinedOb) const;
        WorkerHours operator += (const WorkerHours &combinedOb);
        operator int() const;
        operator double() const;
        friend void showInternalData(string label, const WorkerHours &worker);
        WorkerHours(WorkerHours &janeCpy);
        ~WorkerHours();
 };



WorkerHours::WorkerHours() {
    hoursWorked = 0;
    copies = 0;
}
WorkerHours::WorkerHours(int hoursWork) {
    hoursWorked = hoursWork;
}
WorkerHours::WorkerHours(WorkerHours &janeCpy) {
    hoursWorked = janeCpy.hoursWorked;
    copies = (janeCpy.copies + 1);
}
int WorkerHours::getCpy() {
    return copies;
}
WorkerHours WorkerHours::operator += (const WorkerHours &combinedOb) {
    WorkerHours result;
    hoursWorked += combinedOb.hoursWorked;
    return result;
}
WorkerHours WorkerHours::operator + (const WorkerHours &combinedOb) const {
    WorkerHours result;
    result.hoursWorked = hoursWorked + combinedOb.hoursWorked;
    return result;
}
void WorkerHours::setData(int a) {
    hoursWorked = a;
}
void WorkerHours::setData(double a) {
    hoursWorked = a * HOURS_PER_DAY;
}
WorkerHours::operator int() const {
    return hoursWorked;
}
WorkerHours::operator double() const {
    return (hoursWorked / HOURS_PER_DAY);
}
WorkerHours::~WorkerHours() {
    cout << "Destroyed" << endl;
}

// This is the prototype of the showInternalData function.
// It must access INTERNAL STRUCTURES in the worker object.
// Do NOT use member functions to get the data for that object.
void showInternalData(string label,  WorkerHours &worker);
int main()
{
    // Conversion constructor
    WorkerHours jane = JANE_HOURS, jim = JIM_HOURS;
    // Copy constructor
    WorkerHours janeCopy = jane;
    // + operator
    WorkerHours combined = jane + jim;
    // Default constructor
    WorkerHours testSetters;
    // Variables set aside for calculations
    double daysWorked;
    int hoursWorked;
    // You can use static_cast here, but it shouldn't be required.
    //static_cast<double>(combined);
    // Type conversion operator - int
    daysWorked = combined;
    cout << "TEST DAYS WORKED : " << daysWorked << endl;
    // You can use static_cast here, but it shouldn't be required.
    // static_cast<int>(combined);
    // Type conversion operator - double
    hoursWorked = combined; //using the = already (over loading)
    cout << "TEST HOURS WORKED: " << hoursWorked << endl;
    // Now we start using the internal function
    showInternalData("Jane", jane);
    showInternalData("JaneCopy", janeCopy);
    showInternalData("Jim", jim);
    showInternalData("Combined", combined);
    // += operators
    jane += janeCopy;
    showInternalData("Jane + JaneCopy", jane);
    // Now we test the overloaded setters
    testSetters.setData(SETTER_HOURS);
    showInternalData("Testing int setter", testSetters);
    testSetters.setData(SETTER_DAYS);
    showInternalData("Testing double setter", testSetters);
    // We’re done
    system("pause");
    return 0;
}


void showInternalData(string label, WorkerHours &worker) {
    string x = label;
    cout << "Data: " << x << ", " << "Hours worked:" <<  worker.operator int() << ", " << "Copy generation: "<< worker.getCpy()<< endl;
} 

问题很简单。您的代码运行良好。它是您正在使用的编译器。在那个案例中不要使用VisualC++。Visual C++的操作方式不同。与GCC一样,它使用重载的+运算符,但紧接着,它将+运算符函数的结果对象放入复制构造函数中,然后复制构造函数在结果对象上运行。复制构造函数的结果,而不是重载+,是名为“combined”的变量的结果。

以更清楚地理解问题。你需要知道什么时候该类被复制?或者您需要知道,对于同一个人,例如Jane,该类何时有第二个实例?
TEST DAYS WORKED : 2.08333
TEST HOURS WORKED: 50
Data: Jane, Hours worked: 30, Copy generation: 0
Data: JaneCopy, Hours worked: 30, Copy generation: 1
Data: Jim, Hours worked: 20, Copy generation: 0
Data: Combined, Hours worked: 50, Copy generation: 0
Data: Jane + JaneCopy, Hours worked: 60, Copy generation: 0
Data: Testing int setter, Hours worked: 40, Copy generation: 0
Data: Testing double setter, Hours worked: 72, Copy generation: 0