Tak wiec mam klase szablonowa Plansza<Typ, int x, int y>
Prcy takim kodzie:
Statek* wsk1=NULL;
Plansza<Statek*, sizex, sizey> statki(wsk1);
Plansza<Statek*, sizex, sizey>* wsk=NULL;
wsk=&statki;
wsk->wypisz(); return 0;
wszystko dziala, program zwraca wartosc 0;
Klasa Gracz wyglada tak:
class Gracz
{
public:
int typ; //0- czlowkie, 1-komputer
Plansza<Statek*, 10, 10>* p;
public:
Gracz();
Gracz(int _typ, Plansza<Statek*, 10,10> * mapa);
~Gracz();
Plansza<Statek*, x, y>* getMapa();
};
#include "Gracz.h"
Gracz::Gracz()
{
typ=0;
p=NULL;
}
Gracz::Gracz(int _typ, Plansza<Statek*, 10,10> *mapa)
{
typ=_typ;
p=mapa;
}
Gracz::~Gracz()
{
delete p;
}
Plansza<Statek*, x, y>* Gracz::getMapa()
{
return p;
}
I przy takim kodzie:
Gracz g(1,&statki);
g.getMapa()->wypisz();
program sie uruchamia, mapa zostaje wypisana i wyskakuje error" Wystąpił problem z aplikacją Okrety.exe i zostanie ona zamknięta. Przepraszamy za kłopoty." Nie za bardzo rozumiem czemu tak sie dzieje, skoro jak wywoluje po prostu w mainie to wszystko gra, a jak juz przekazuje przez funkcje to zaczyna sie sypac.
to moze jeszcze dodam konstrukcje szablonu:
#ifndef PLANSZA_H
#define PLANSZA_H
#include <iostream>
using namespace std;
template<typename T, int x, int y>
class Plansza
{
private:
//int szer, wys;
T** plansza;
public:
Plansza() {};
Plansza(T & typ);
~Plansza();
void setElement(T typ, int _x, int _y);
T getElement(int _x, int _y);
void wypisz();
//friend void dodajElement(T& typ, int _x, int _y);
};
template<typename T, int x, int y>
Plansza<T, x, y>::Plansza(T& typ)
{
plansza=new T*[x];
for (int i=0; i<x; i++)
{
plansza[i]=new T[y];
}
for (int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
plansza[i][j]=typ;
}
}
}
template<typename T, int x, int y>
Plansza<T, x, y>::~Plansza()
{
for(int i=0; i<x; i++)
{
delete plansza[i];
}
delete [] plansza;
}
template<typename T, int x, int y>
void Plansza<T, x, y>::setElement(T typ, int _x, int _y)
{
plansza[_x][_y]=typ;
}
template<typename T, int x, int y>
T Plansza<T, x, y>::getElement(int _x, int _y)
{
return plansza[_x][_y];
}
template<typename T, int x, int y>
void Plansza<T, x, y>::wypisz()
{
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
cout<<plansza[i][j]<<" ";
}
cout<<endl;
}
}
#endif