operators overloading, strange seg fault


Hello!

I was given an exercise (the main() part) by my teacher and was supposed to write functions so that it would work. But I keep having seg fault. It's ok if the

TSeries series4=series1(2,4);
cout<<"Series4: "<<series3<<endl;
lines are commented. I would be grateful for your help.





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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    #include <iostream>
    class TSeries{
        public:
            TSeries()
                {
                     _size = 0;
                     _capacity = 0;
                     _tab = NULL;
                }
    
    
            TSeries(float *tab, const int size)
            {
                     _tab = new float[size];
                    for(int i =0;i<size;i++) _tab[i] = tab[i];
                    _size = size;
            }
    
            ~TSeries(){delete [] _tab;}
    
            TSeries & operator+=(float value){return insert(value);}
            TSeries & operator,(float value){return insert(value);}
            TSeries & operator+(const TSeries & s)
            {  
              //  if(this->_size != s._size) std::cout<<"Size doesn't match!"<<std::endl;
                /*else
                {
                    std::cout<<"whee";
                    for(int i; i<this->_size;i++)
                    {
                        //this->_tab[i] += s._tab[i];
                        std::cout<<"nothing";
                    }
                return *this;
                }*/
            //std::cout<<"sth";
            }
    
            TSeries & operator()(int position1, int position2)
            {
               // return *this;
            }
    
            TSeries & insert(float k)
                {
                    if(_size >= _capacity) Enlarge();					
                    _tab[_size++] = k;
    	            return *this;
                }
            friend std::ostream & operator<<(std::ostream & out, const TSeries & s);
       
    
        private:
            int _size, _capacity;
            float *_tab, *_itr;
            static int _nr;
    
    			void Enlarge()
    			{
    				_capacity = 2 * _capacity + 1; 
    				float *tmp = new float[_capacity]; 
    				
    				for( int i=0;i<_size;++i)
    				{
    					tmp[i] = _tab[i];
    				}
    				delete [] _tab;
    				_tab = tmp;
    			}
    };
    
    std::ostream & operator<<(std::ostream & out, const TSeries & s)
    {
        int przedostatni = s._size - 1;
        out<<"(";
        for(int i =0;i<s._size;i++)
        {  
            out<<(int)s._tab[i];
            if(i != przedostatni)
                out<<",";
        }
        out<<")"<<std::endl;
    }
    
    using namespace std;
    int main(int argc, char **argv) {
      TSeries series1;
      series1 += 1.,2.,4.,2.;
      cout<<"Series1: "<<series1<<endl;
     
      const int size=7;
      float tab[size] = {3.,3.,3.,4.,5.,1.,0.}; 
      const TSeries series2(tab,size);
      cout<<"Series2: "<<series2<<endl<<endl;
    
     
      TSeries series3 = series1+series2;
      cout<<"Series3: "<<series3<<endl<<endl;
      
      series1+=1.,0.,3.;
      series3=series1+series2;
      cout<<"           "<<series1<<endl;
      cout<<"          +"<<series2<<endl;
      cout<<"        ---------------------"<<endl;
      cout<<"Series3:   "<<series3<<endl<<endl;
      
      TSeries series4=series1(2,4);
      cout<<"Series4: "<<series3<<endl;
      
      return 0;
    }

What are you trying to achieve with this line:
TSeries series4=series1(2,4);
?

Also, why are you trying to create a series4 variable when you don't even use it? (you're using series3 on the line after this).
The problem is the constructor on line12: You do not set _capacity. It remains uninitialized.
Mutexe - my mistake, I was trygin to check sth, in original code the last line was:
cout<<"Serires4: "<<seires4<<endl;
And the main() was given by my tutor - the point of this exercise is overloading, not logic ;)
Coder777 - I've followed your advice and changed the construkctor. Seg fault remains.

1
2
3
4
5
6
7
        TSeries(float *tab, const int size)
        {
                 _tab = new float[size];
                for(int i =0;i<size;i++) _tab[i] = tab[i];
                _size = size;
                _capacity = 0;
        }
_capacity must not be 0, it must be at least _size.

Line 97/107 is a problem. You make a shallow copy due to the lack of a copy constructor/operator. The program will crash because it tries to free the same memory twice.
Last edited on
Unfortunately it's still not helping.

1
2
3
4
5
6
7
       TSeries(float *tab, const int size)
        {
                 _tab = new float[size];
                for(int i =0;i<size;i++) _tab[i] = tab[i];
                _size = size;
                _capacity = _size;
        }


Any other suggestions?
Where exactly does it crash? The best would be to use the debugger, but you might use some debugging cout.

Obviously your code is not complete. What about the operator+ on line 23 or the operator() on line 39? You use both.
Topic archived. No new replies allowed.