#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class BladLamana:std::exception
{
public:
virtual const char* what()
{
return "Blad Lamanej";
}
};
template <class x,class y>class lamana
{
private:
int lwierzcholkow;
public:
struct wierzcholek
{
x wartoscx;
y wartoscy;
int w;
wierzcholek():wartoscx(0),wartoscy(0)
{
}
wierzcholek(x x,y y):wartoscx(x),wartoscy(y)
{
}
};
vector<wierzcholek> tmp;
lamana(int liczba):lwierzcholkow(liczba)
{
for (int i=0;i<5;i++)
{
tmp.push_back(lamana<x,y>::wierzcholek(0,0));
}
}
lamana& operator [] (int indeks);
template<class x,class y> friend ostream& operator << (ostream&,lamana<x,y>&);
template<class x,class y> friend istream& operator >> (istream&,lamana<x,y>&);
};
template <class x, class y>
lamana<x, y>& lamana<x, y>:: operator [] (int indeks)
{
if (indeks<1 ||indeks>lwierzcholkow-1) throw BladLamana();
return tmp[indeks];
}
template <class x,class y> ostream& operator << (ostream& os,lamana<x,y>& l)
{
os<<l.lwierzcholkow<<endl;
for (int i=0;i<l.lwierzcholkow;i++)
{
os<<"("<<l.tmp[i].wartoscx<<","<<l.tmp[i].wartoscy<<")";
}
return os;
}
template <class x,class y> istream& operator >> (istream& is,lamana<x,y>& w)
{
return is;
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
string name="lamana.txt";
lamana<int,int> jeden(5);
ofstream out(name.c_str(),ios::out);
out<<jeden;
for (int i=0;i<jeden.tmp.size();i++)
{
cout<<jeden.tmp[i].wartoscx<<jeden.tmp[i].wartoscy<<endl;
}
jeden[0].tmp[0].wartoscx=5;
}
catch(BladLamana& e)
{
cerr<<"Wystapil blad:"<<e.what()<<endl;
}
system ("pause");
return 0;
}
Czy jest mozliwa modyfikacja operatora indeksowania w taki spsob aby otrzymac:
jeden[i]=(5,3);
Modyfikacja tmp[i].wierzcholekx oraz tmp[i].wierzcholeky???