【问题标题】:Inherited classes won't write to file [closed]继承的类不会写入文件[关闭]
【发布时间】:2014-04-14 18:35:02
【问题描述】:

所以,我的继承类将项目写入文件时遇到问题。我将发布代码示例,因为它有多个实例,它不会将这些特定项目写入文件。我不确定我做错了什么,它将父类中的所有内容都写入文件中,但子类中的那些东西不会写入。这部分已经在每个子类中完成了:

//Parameterized constructor signature
Hourly::Hourly(int e, string n, string a, string p, double w, double h) :MyEmployee(e, n, a, p)

这是没有标题的驱动程序。

string fileInput;
    string employCategory = "";
    const string HOURLY = "Hourly";
    const string SALARIED = "Salaried";
    int answer;
    int count = 0;
    const int ONE = 1;
    const int TWO = 2;
    const int ARRAY_SIZE = 4;
    MyEmployee* payroll[ARRAY_SIZE];
    ifstream myOpenFile;
    ofstream myWrittenFile;
    payroll[0] = new Hourly(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00);
    payroll[1] = new Hourly(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00);
    payroll[2] = new Salaried(2, "A. Dumbledore", "Hogwarts", "803-1230", 1200);
    payroll[3] = new Salaried(4, "R. Hagrid", "Hogwarts", "910-8765", 1000);
    cout << "This program has two options:\n1 - Create a data file\n2 - Read data from a file and print paychecks.";
    //This loop will test if the user's input is valid.
    do
    {
        //Here, the user will enter a value to be used to either print checks or write a file.
        cout << "\nPlease enter <1> to create a file or <2> to print checks: ";
        cin >> answer;
        //The user entered one, so we'll write a file.
        if (answer == ONE)
        {
            cin.sync();
            cin.clear();
            cout << "\nPlease enter in the name of the file you wish to write to. Please don't forget to add .txt to the end: ";
            getline(cin, fileInput);
            myWrittenFile.open(fileInput);
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                MyEmployee* empPtr = payroll[i];
                if (typeid(*empPtr) == typeid(Hourly))
                {
                    Hourly* empHPtr = static_cast<Hourly*>(empPtr);
                }
                else if (typeid(*empPtr) == typeid(Salaried))
                {
                    Salaried* empSPtr = static_cast<Salaried*>(empPtr);
                }
            }
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                payroll[i]->writeData(myWrittenFile);
            }
            myWrittenFile.close();
            cout << "\nData saved .....";
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                delete payroll[i];
                payroll[i] = NULL;
            }


}

这是父类的一部分,其中包括写入文件:

void MyEmployee::writeData(ofstream& out)
{
    out << empNum << "\n";
    out << name << "\n";
    out << address << "\n";
    out << phoneNum << "\n";
}

这是子类的一部分:

void Hourly::writeData(ofstream& out)
{
    out << hoursWorked << "\n";
    out << wage << "\n";
    MyEmployee::writeData(out);
}

【问题讨论】:

  • 在不确切了解您的类声明的情况下,这有点难以判断,但您是否正确使用 virtual 声明了 writeData(ofstream&amp; out) 成员函数?
  • 为什么payroll 使用动态内存?这不是 Java 或 C#。仅在可以证明合理时才使用动态内存。
  • @ThomasMatthews 理由是利用多态性(必须通过引用或指针完成)并避免对象切片,因为仅存储 MyEmployee 会在分配期间无意中剪切扩展类内容。虽然我同意 OP 最好将 std::unique_ptr&lt; MyEmployee&gt; 用于内存元素,但对于正在尝试的内容,some 形式的动态管理仍然是必需的。

标签: c++ inheritance stream


【解决方案1】:

似乎,这是因为您的 writeData() 函数未声明为虚拟函数。

当一个函数被声明为虚拟函数并在派生类中被覆盖时,它将在运行时计算出该函数必须被准确调用的内容。 (Virtual method tables)。

您的函数未声明为虚拟函数,因此从MyEmployee 指针调用它,您调用的是在MyEmployee 类中定义的writeData() 函数,而不是在其派生类中。这就是它不会将派生类中的任何数据写入文件的原因。

PS:

typeid(*empPtr) == typeid(Hourly)
typeid(*empPtr) == typeid(Salaried)

这些表达式将始终为false

【讨论】:

  • 不可能……这是老师告诉我们的。什么最有效?
  • @nelson2013 是关于虚函数的问题,还是typeid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
相关资源
最近更新 更多