重写方法错误C ++

时间:2013-01-08 02:38:41

标签: c++ exception inheritance visual-studio-2012

我正在编写一个程序,我在其中继承了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.如果有人可以帮我弄清问题是什么,我们将不胜感激

1 个答案:

答案 0 :(得分: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()];涉及对月份的大的未初始化垃圾值的数组访问权限指数。 因此崩溃

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

addDays也无法像您在Java中所期望的那样工作。您需要阅读名为Virtual Functions的概念。默认情况下为all (non-static) functions are virtual in Java,但您需要通过执行以下操作在C ++中手动将它们设为虚拟

class dateType
{

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

};