Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Nieobsługiwany wyjątek

Ostatnio zmodyfikowano 2017-05-11 16:25
Autor Wiadomość
klaphson
Temat założony przez niniejszego użytkownika
Nieobsługiwany wyjątek
» 2017-05-10 23:35:19

Ktoś ma jakiś pomysł o co może chodzić?

Zgłoszono nieobsługiwany wyjątek: naruszenie dostępu do odczytu.
**this** było 0xCCCCCCCC.: wystąpił wyjątek
P-160925
maly7
» 2017-05-10 23:37:45
Pokaż kod, wiele rzeczy może być przyczyną.
P-160926
klaphson
Temat założony przez niniejszego użytkownika
» 2017-05-10 23:45:30
Gdy dam debuguj to zaznacza mi ze niobslugiwany wyjatek przy returnie w pliku Talia.cpp w funkcji DajKArte

Talia.cpp
C/C++
#include "Talia.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


CTalia::CTalia( void )
{
    int pom = 0;
    for( int i = 0; i < 52; i++ )
    {
       
        ptrTabKart[ i ] = new CKarta;
        if( i < 2 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 2 );
            ptrTabKart[ i ]->UstawNazwe( '2' );
        }
        else if( 1 < i && i < 4 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 2 );
            ptrTabKart[ i ]->UstawNazwe( '2' );
        }
        else if( 3 < i && i < 6 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 3 );
            ptrTabKart[ i ]->UstawNazwe( '3' );
        }
        else if( 5 < i && i < 8 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 3 );
            ptrTabKart[ i ]->UstawNazwe( '3' );
        }
        else if( 7 < i && i < 10 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 4 );
            ptrTabKart[ i ]->UstawNazwe( '4' );
        }
        else if( 9 < i && i < 12 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 4 );
            ptrTabKart[ i ]->UstawNazwe( '4' );
        }
        else if( 11 < i && i < 14 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 5 );
            ptrTabKart[ i ]->UstawNazwe( '5' );
        }
        else if( 13 < i && i < 16 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 5 );
            ptrTabKart[ i ]->UstawNazwe( '5' );
        }
        else if( 15 < i && i < 18 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 6 );
            ptrTabKart[ i ]->UstawNazwe( '6' );
        }
        else if( 17 < i && i < 20 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 6 );
            ptrTabKart[ i ]->UstawNazwe( '6' );
        }
        else if( 19 < i && i < 22 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 7 );
            ptrTabKart[ i ]->UstawNazwe( '7' );
        }
        else if( 21 < i && i < 24 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 7 );
            ptrTabKart[ i ]->UstawNazwe( '7' );
        }
        else if( 23 < i && i < 26 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 8 );
            ptrTabKart[ i ]->UstawNazwe( '8' );
        }
        else if( 25 < i && i < 28 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 8 );
            ptrTabKart[ i ]->UstawNazwe( '8' );
        }
        else if( 27 < i && i < 30 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 9 );
            ptrTabKart[ i ]->UstawNazwe( '9' );
        }
        else if( 29 < i && i < 32 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 9 );
            ptrTabKart[ i ]->UstawNazwe( '9' );
        }
        else if( 31 < i && i < 34 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 10 );
            ptrTabKart[ i ]->UstawNazwe( 'D' );
        }
        else if( 33 < i && i < 36 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 10 );
            ptrTabKart[ i ]->UstawNazwe( 'D' );
        }
        else if( 35 < i && i < 38 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 2 );
            ptrTabKart[ i ]->UstawNazwe( 'J' );
        }
        else if( 37 < i && i < 40 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 2 );
            ptrTabKart[ i ]->UstawNazwe( 'J' );
        }
        else if( 39 < i && i < 42 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 3 );
            ptrTabKart[ i ]->UstawNazwe( 'Q' );
        }
        else if( 41 < i && i < 44 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 3 );
            ptrTabKart[ i ]->UstawNazwe( 'Q' );
        }
        else if( 43 < i && i < 46 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 4 );
            ptrTabKart[ i ]->UstawNazwe( 'K' );
        }
        else if( 45 < i && i < 48 )
        {
            ptrTabKart[ i ]->UstawKolor( 'B' );
            ptrTabKart[ i ]->UstawWartosc( 4 );
            ptrTabKart[ i ]->UstawNazwe( 'K' );
        }
        else if( 47 < i && i < 50 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 11 );
            ptrTabKart[ i ]->UstawNazwe( 'A' );
        }
        else if( 49 < i && i < 52 )
        {
            ptrTabKart[ i ]->UstawKolor( 'R' );
            ptrTabKart[ i ]->UstawWartosc( 11 );
            ptrTabKart[ i ]->UstawNazwe( 'A' );
        }
    }
   
}


CTalia::~CTalia( void )
{
    for( int i = 0; i < 52; i++ )
    {
        delete ptrTabKart[ i ];
        ptrTabKart[ i ] = NULL;
    }
}

void CTalia::DrukujTalie()
{
    char a, b;
    int c;
    for( int i = 0; i < 52; i++ )
    {
        a = ptrTabKart[ i ]->DajKolor();
        b = ptrTabKart[ i ]->DajNazwe();
        c = ptrTabKart[ i ]->DajWartosc();
        cout << "Karta: " << b << " Kolor: " << a << " Wartosc: " << c << endl;
    }
}

CKarta * CTalia::DajKArte( void )
{
    //char a, b;
    //int c;
    //a = ptrTabKart[0]->DajKolor();
    //b = ptrTabKart[0]->DajNazwe();
    //c = ptrTabKart[0]->DajWartosc();
    //cout << "Dostales: " << b << " Kolor: " << a << " Wartosc: " << c << endl;
   
    return ptrTabKart[ pom ];
}

void CTalia::Tasuj( void )
{
    srand( time( NULL ) );
    for( int i = 0; i < 52; i++ )
    {
        int wylosowana_liczba1 =( rand() % 52 );
        int wylosowana_liczba2 =( rand() % 52 );
        CKarta * wsk_pom;
        wsk_pom = ptrTabKart[ wylosowana_liczba1 ];
        ptrTabKart[ wylosowana_liczba1 ] = ptrTabKart[ wylosowana_liczba2 ];
        ptrTabKart[ wylosowana_liczba2 ] = wsk_pom;
    }
   
   
}

Talia.h
C/C++
#pragma once
#include "Karta.h"
class CTalia

{
public:
    CTalia();
    ~CTalia();
   
   
    CKarta * ptrTabKart[ 52 ];
    int pom;
   
   
    void Tasuj( void );
    CKarta * DajKArte();
    void DrukujTalie( void );
   
};

Karta.cpp
C/C++
#include "Karta.h"



CKarta::CKarta()
{
}


CKarta::~CKarta()
{
}


int CKarta::DajWartosc( void )
{
    return Wartosc;
}

char CKarta::DajKolor( void )
{
    return Kolor;
}

void CKarta::UstawKolor( char Kol )
{
    Kolor = Kol;
}

void CKarta::UstawWartosc( int Wart )
{
    Wartosc = Wart;
}

void CKarta::UstawNazwe( char Naz )
{
    Nazwa = Naz;
}

char CKarta::DajNazwe( void )
{
    return Nazwa;
}

CKarta & CKarta::operator =( CKarta & KartaPrzyrownywana )
{
    UstawKolor( KartaPrzyrownywana.DajKolor() );
    UstawNazwe( KartaPrzyrownywana.DajNazwe() );
    UstawWartosc( KartaPrzyrownywana.DajWartosc() );
    return( * this );
}

Karta.h
C/C++
#pragma once
class CKarta
{
public:
    CKarta();
    ~CKarta();
   
   
    int DajWartosc();
    char DajKolor();
    char DajNazwe();
    void UstawWartosc( int Wartosc );
    void UstawKolor( char Kolor );
    void UstawNazwe( char Nazwa );
    CKarta & operator =( CKarta & KartaPrzyrowynywana );
   
private:
    int Wartosc;
    char Kolor;
    char Nazwa;
};

Źródło.cpp
C/C++
#include "Talia.h"
#include "Gracz.h"

void main( void )
{
    CTalia a;
    CGracz g;
    g.Graj();
}

Gracz.cpp
C/C++
#include "Gracz.h"
#include <iostream>
using namespace std;




CGracz::CGracz()
{
    Stan = 0;
    for( int i = 0; i < 10; i++ )
    {
       
        ptrKartyGracza[ i ] = new CKarta;
        ptrKartyGracza[ i ] = NULL;
    }
   
}


CGracz::~CGracz()
{
    delete ptrKartyGracza[ 0 ];
}

void CGracz::Graj()
{
   
    ptrKartyGracza[ 0 ] = talia->DajKArte();
}

void CGracz::UstawStan( int a )
{
    Stan = a;
}

int CGracz::DajStan( void )
{
    return Stan;
}

Gracz.h
C/C++
#pragma once
#include "Karta.h"
#include "Talia.h"
#include <iostream>

class CGracz
{
public:
    CGracz();
    ~CGracz();
   
    CKarta * ptrKartyGracza[ 10 ];
    CTalia * talia;
   
    void Graj( void );
    void UstawStan( int Stan );
    int DajStan( void );
   
private:
    int Stan;
};

P-160927
croppp
» 2017-05-11 00:11:14
Albo jest już późno albo ja nie rozumiem sensu tego zapisu.

C/C++
CGracz::CGracz()
{
    Stan = 0;
    for( int i = 0; i < 10; i++ )
    {
       
        ptrKartyGracza[ i ] = new CKarta;
        ptrKartyGracza[ i ] = NULL;
    }
   
}
P-160929
klaphson
Temat założony przez niniejszego użytkownika
» 2017-05-11 00:29:55
z tym czy bez tego nie ma różnicy dalej nie działa
P-160931
darko202
» 2017-05-11 08:17:39
z
http://cpp0x.pl/kursy/Kurs-C++​/Wskazniki/304
"... Pamiętaj gdy używasz wskaźnika zadbaj o to by zawsze miał przypisaną zmienną(miał prawidłowy adres)..."

zastanów się dlaczego autor artykułu tak mocno to podkreśla

nie wiem czy dobrze to opiszę, ale
jeśli w zamieszczonym tam przykładzie
C/C++
...
daneST dane;
dane.liczba = 344;
daneST * wskaznik = & dane;
cout << "wskaznik->liczba=" << wskaznik->liczba << endl;
// było dobrze

//to po zmianie
daneST * wskaznik = NULL;
// spodziewam się że w tej linii pojawi się wyjątek
cout << "wskaznik->liczba=" << wskaznik->liczba << endl;

//dokładnie w  wskaznik->liczba 

bo "->" to odwołanie od adresu od adresu, ale adresu startowy jest nieokreślony

https://msdn.microsoft.com​/en-us/library/b930c881.aspx
P-160933
klaphson
Temat założony przez niniejszego użytkownika
» 2017-05-11 14:39:02
Czyli gdzie byś widział jakiś błąd w moim kodzie?
P-160944
Luq
» 2017-05-11 16:25:10
A na co wskazuje
CTalia * talia
 w obiekcie klasy CGracz?
P-160948
« 1 »
  Strona 1 z 1