problem with ostream operator
May 10, 2010 at 2:06pm UTC
Hello! that's my program:
.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Tablica::Tablica(int rozmiar)
{
if (rozmiar<0) ;
if (rozmiar==0) ;
if (rozmiar<10) ;
if (rozmiar>1000) ;
bufor=new int [rozmiar];
}
Tablica::~Tablica()
{
delete []bufor;
}
Tablica& Tablica::operator =(const Tablica &tab)
{
if (this ==&tab) return *this ;
rozmiar=tab.PobierzRozmiar();
delete [] bufor;
bufor=new int [rozmiar];
for (int i=0; i<rozmiar; i++)
{
bufor[i]=tab.bufor[i];
}
return *this ;
}
int & Tablica::operator [](int indeks)
{
int rozmiar = PobierzRozmiar();
if (indeks>=0 && indeks<PobierzRozmiar()) return bufor[indeks];
//throw A();
}
ostream& operator <<(ostream& out, const Tablica &tab)
{
for (int i =0; i<(tab.PobierzRozmiar()); i++)
{
out<< "[" << i<< "] " <<tab.bufor[i]<<endl;
}
return out;
}
.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
using namespace std;
class Tablica
{
private :
int *bufor;
int rozmiar;
public :
Tablica(int rozmiar);
~Tablica();
int PobierzRozmiar() const {return rozmiar;}
Tablica &operator =(const Tablica&);
int & operator [](int indeks);
friend ostream& operator <<(ostream&, const Tablica&);
};
main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include "xyz.h"
using namespace std;
int main()
{
Tablica new_tab(5);
cout<<new_tab;
getch();
return 0;
}
when i'm compiling this program it's displaying not 5 but 2700 positions and then error appears...i don't know what is wrong in definition of << operator..can somebody help?
sorry for english...
Last edited on May 10, 2010 at 2:06pm UTC
May 10, 2010 at 2:27pm UTC
In your constructor Tablica::Tablica(int rozmiar)
, you never initialise member variable rozmiar
Topic archived. No new replies allowed.