0

我正在编写一个程序,在该程序中我从 C++ 中的另一个类继承并重写了几个方法。因为我只想在这些方法中添加一行,所以我尝试在基类中调用该方法并在其下方添加一行。我收到以下错误。

"Unhandled exception at 0x00FA4456 in Ch.12.exe: 0xC0000005: Access violation reading location 0x67525A64."  

这是我在java中最接近“超级”的(据我所知)。

类的简化版

using namespace std;

#include <iostream>
#include <string>
#include "dateType2.h"

class extDateType : public dateType
{
private:
    string monthString;
    void updateMonthString()
    {
        string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        monthString = months[getMonth()];
    }
public:
void printDateString()
{
    string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    cout << months[getMonth()];
    cout << " ";
    cout << getYear();
}
extDateType(int month, int day, int year)
{
    dateType(month, day, year);
    updateMonthString();
}
void addDays(int x)
{
    dateType::addDays(x);
    updateMonthString();
}
};

int main()
{
    extDateType x(2, 25, 1996);
    x.addDays(10);
    x.printDateString();
    system("pause");
    return 0;
}

基类的简化版本

using namespace std;

#include <iostream>
#include <string>

class dateType
{
private:
    int dMonth;
    int dDay;
    int dYear;
    bool isLeapYear;
public:
    dateType(){}
    void addDays(int x)
    {
        int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        dDay += x;
        for(int i = dMonth-1; dDay > days[i]; i++)
        {
            dDay -= days[i];
            dMonth++;
            if(dMonth == 13)
            {
                dMonth = 0;
                dYear++;
            }
            i = i % 11;
        }
    }
    dateType(int month, int day, int year)
    {
        isLeapYear = false;
        int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(month > 0 && month <= 12)
        {
            if(month == 2)
            {
                    if(year % 4 == 0)
                {
                    isLeapYear = true;
                    if(day > 0 && day <= 29)
                    {
                        dMonth = month;
                        dDay = day;
                        dYear = year;
                    }
                    else
                    {
                        cout << "Error" << endl;
                        dMonth = 1;
                        dDay = 1;
                        dYear = 2000;
                    }
                }
                else
                {
                    isLeapYear = false;
                    if(day > 0 && day <= 28)
                    {
                        dMonth = month;
                        dDay = day;
                        dYear = year;
                    }
                    else
                    {
                        cout << "Error" << endl;
                        dMonth = 1;
                        dDay = 1;
                        dYear = 2000;
                    }
                }
            }
            else
            {
                if(day > 0 && day <= days[month-1])
                {
                    dMonth = month;
                    dDay = day;
                    dYear = year;
                }
                else
                {
                    cout << "Error" << endl;
                        dMonth = 1;
                    dDay = 1;
                    dYear = 2000;
                }    
            }
        }
        else
        {
                cout << "Error" << endl;
            dMonth = 1;
            dDay = 1;
            dYear = 2000;
        }
    }
};

使用 Visual Studio 2012。如果有人能帮我找出问题所在,将不胜感激

4

1 回答 1

5
extDateType(int month, int day, int year)
{
    dateType(month, day, year);

这是解决您的问题的方法,将上面的内容替换为下面的内容。

extDateType(int month, int day, int year)
:dateType(month, day, year)
{

前者只是创建一个未使用的临时对象,然后在执行该行后将其销毁,而后者正确调用基类构造函数。dateType(){}如果您删除了会导致编译器错误的 ,这可能会更明显。

由于未正确调用基类构造函数,因此在updateMonthString调用时,该行涉及对月份的大型未初始化垃圾值作为索引monthString = months[getMonth()];的数组访问。从而崩溃

后一种语法称为初始化列表是初始化没有默认(无参数)构造函数的基类和成员对象的正确方法。

addDays不会像您在 Java 中所期望的那样工作。您需要阅读名为Virtual Functions的概念。默认情况下,所有(非静态)函数都virtual在 Java 中,但您需要通过执行以下操作在 C++ 中手动使它们成为虚拟函数

class dateType
{

    ...
    virtual void addDays(int x){
  //^^^^^^^
    ...

};
于 2013-01-08T02:51:51.180 回答