ninigi
ninigi

Reputation: 143

C++, classes, Segmentation fault, all over the programme

I got an exercise form my teacher (the main() function) and was supposed to write functions and classes so that it would work. But I have seg fault and have no idea what to do about it. I would be grateful for any recommendations.

#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){}
        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;
}

Upvotes: 0

Views: 33

Answers (1)

The Dark
The Dark

Reputation: 8514

You fixed one of your problems (the assignment to the _tab pointer).

The other problem should have caused a warning from the compiler.

You need to return out from your operator<< method.

Note that you also should use the out parameter, rather than always using cout in the the method.

Upvotes: 1

Related Questions