Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 4 của 4
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    khắc phục lỗi 0xC0000005 Access Violation. Xin hướng dẫn

    Mình mới học C++, khi viết bài về lớp Cdate thi bị lỗi 0xC0000005 Access Violation, ai giúp mình tìm ra chỗ bị lỗi và cách đê sửa nó ra sao với
    Mã:
    #include "stdafx.h"
    #include <iostream.h>
    #include <conio.h>
    class Cdate
    {
    private:
    	int Day,Month;
    	int Year;
    	static int days[];
    	void Inc(); 
    	void Dec(); 
    public:
    	Cdate(int Da=1,int Mo=1,int Ye=90);
    	void Setday(int,int,int);
    	Cdate operator++(); 
    	Cdate operator++(int);
    	Cdate operator--(); 
    	const Cdate &operator+=(int);
    	int leapyear(int);
    	int endofmonth(int);
    	friend ostream &operator<<(ostream&,const Cdate&);
    };
    int Cdate::days[] = {31,28,31,30,31,3031,31,30,31,30,31};
    void Cdate::Setday(int dd,int mm,int yy)
    {
    	mm=(mm>=1&&mm<=12)?mm:1;
    	yy=(yy>=1594&&yy<=2400)?yy:1990;
    	if(mm==2&&leapyear(yy))
    		dd=(dd>=1&&dd<=29)?dd:1;
    	else
    		dd=(dd>=1&&dd<=28)?dd:1;
    }
    
    Cdate::Cdate(int d,int m,int y)
    {
    	Setday(d,m,y);
    }
    int Cdate::leapyear(int y)
    {
    	if(y%100==0&&y%4||y%400)
    		return 1;
    	return 0;
    }
    
    int Cdate::endofmonth(int D)
    {
    	if(Month==2&&leapyear(Year))
    		return D=29;
    	return D=days[Month-1];
    }
    
    void Cdate::Inc()
    {
    	if(endofmonth(Day)&&Month==12) //End of year
    	{
    		Day=1;
    		Month=1;
    		Year++;
    	}
    	else
    	{
    		if(endofmonth(Day)) //End of Month
    		{
    			Day=1;
    			Month++;
    		}
    		else
    			Day++;
    	}
    }
    void Cdate::Dec()
    {
    	if(Day==1&&Month==3&&leapyear(Year))
    	{
    		Day=28;
    		Month=2;
    	}
    	if(Day==1&&Month==1)
    	{
    		Day=31;
    		Month=12;
    		Year--;
    	}
    	else
    		Day--;
    }
    Cdate Cdate::operator++()
    {
    	Inc();
    	return *this;
    }
    Cdate Cdate::operator++(int)
    {
    	Cdate temp = *this;
    	Inc();
    	return temp;
    }
    Cdate Cdate::operator --()
    {
    	Dec();
    	return *this;
    }
    const Cdate &Cdate::operator+=(int n)
    {
    	for(int i=1;i<n;i++)
    		Inc();
    	return *this;
    }
    ostream &operator<<(ostream &Output, const Cdate &D)
    
    {
    	static char* MonthName[12]={"January","February","March","April","May","June","July", "August","September","October","November", "December" };
    	Output << MonthName[D.Month-1] << ' '<< D.Day << ", " << D.Year;
    	return Output;
    }
    int main()
    {
    	Cdate D1,D2(12,27,1992),D3(0,99,8045);
    	cout << "D1 is " << D1 << endl<< "D2 is " << D2 << endl<< "D3 is " << D3 << endl << endl;
    	cout << "D2 += 7 is " << (D2 += 7) << endl << endl;
    	D3.Setday(2, 28, 1992);
    	cout << " D3 is " << D3 << endl;
    	cout << "++D3 is " << ++D3 << endl << endl;
    	Cdate D4(3, 18, 1969);
    	cout << "Testing the preincrement operator:" << endl << " D4 is " << D4 << endl;
    	cout << "++D4 is " << ++D4 << endl;
    	cout << " D4 is " << D4 << endl << endl;
    	cout << "Testing the postincrement operator:" << endl << " D4 is " << D4 << endl;
    	cout << "D4++ is " << D4++ << endl;
    	cout << " D4 is " << D4 << endl;
    	return 0;
    }
    thanks mọi người

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi ngthtg735252
    Hi, cảm ơn bạn nhiều lắm, cho mình hỏi thêm là lỗi này phát sinh khi nào vậy?
    Lỗi này là do bạn chương trình của bạn truy xuất vào địa chỉ không hợp lệ( không đc phép truy xuất.
    Cụ thể chỗ này:
    Mã nguồn PHP:
    ostream &operator<<(ostream &Output, const Cdate &D) { static char* MonthName[12]={"January","February","March","April","May","June","July", "August","September","October","November", "December" }; Output << MonthName[D.Month-1] << ' '<< D.Day << ", " << D.Year; return Output; }  
    Bài này của bạn có khi cho D.Month-1>11 nên mới xảy ra lỗi(Cụ thể là do thằng constructor của bạn làm sai)

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hi, cảm ơn bạn nhiều lắm, cho mình hỏi thêm là lỗi này phát sinh khi nào vậy?

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    không hiểu ý bạn luôn

    void Cdate::Setday(int dd,int mm,int yy)
    {
    mm=(mm>=1&&mm<=12)?mm:1;
    yy=(yy>=1594&&yy<=2400)?yy:1990;
    if(mm==2&&leapyear(yy))
    dd=(dd>=1&&dd<=29)?dd:1;
    else
    dd=(dd>=1&&dd<=28)?dd:1;
    }
    cái này nè
    Mã nguồn PHP:
    void Cdate::Setday(int dd,int mm,int yy){ Month=(mm>=1&&mm<=12)?mm:1; Year=(yy>=1594&&yy<=2400)?yy:1990; if(mm==2&&leapyear(yy)) Day=(dd>=1&&dd<=29)?dd:1; else Day=(dd>=1&&dd<=28)?dd:1;}  
    bạn đang sử dụng Day,Month,Year mà. sao lại lưu nó vào mm,yy, dd làm chi.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •