运算符重载,奇数段错误,类,c ++

时间:2015-11-10 09:20:19

标签: c++ class segmentation-fault operator-overloading

我的导师给我做了超载操作员的练习。他给了我一个无法更改的int main()函数。我应该编写函数等,以便代码可以工作。不幸的是我有一个seg错误。

我已经注意到了
 TSeries series4=series1(2,4);
 cout << "Series4: " << series4 << endl;

行评论它或多或少有效。我非常感谢你的帮助。

#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;
        _capacity = 0;
    }

    ~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: " << series4 << endl;

    return 0;
}

/* output required:
Series1: (1,2,4,2)
Series2: (3,3,3,4,5,1,0)

Size doesn't match!
Series3: ()

(1,2,4,2,1,0,3)
+(3,3,3,4,5,1,0)
---------------------
Series3:   (4,5,7,6,6,1,3)

Series4: (4,2)
*/

1 个答案:

答案 0 :(得分:3)

  1. intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult(intent, 99); 不会返回任何内容。请在功能结束时添加std::ostream & operator<<(std::ostream & out, const TSeries & s)
  2. return outoperator()都应以operator+
  3. 结尾
  4. 正如 Devolus 已经指出的那样:你没有复制构造函数而且没有定义return *this
  5. 您可以使用operator=(const TSeries&)更快地复制数组。#
  6. memcpy中for循环中的i未初始化。
  7. 您的operator+(const TSeries&)当前确实改变了原始对象。这看起来不对。
  8. <强>代码:

    operator(int, int)

    <强>输出

    #include <iostream>
    class TSeries {
    public:
        TSeries()
        {
            _size = 0;
            _capacity = 0;
    
            _tab = NULL;
        }
    
    
        TSeries(float *tab, const int size)
        {
            _size = size;
            _capacity = 0;
    
            _tab = new float[size];
            memcpy(_tab, tab, _size*sizeof(float));
        }
    
        TSeries(const TSeries& other)
        {
            _size = other._size;
            _capacity = other._capacity;
    
            _tab = new float[_size];
            memcpy(_tab, other._tab, _size*sizeof(float));
        }
    
        ~TSeries() 
        { 
            delete[] _tab;
        }
    
        TSeries & operator+=(float value) { return insert(value); }
        TSeries & operator,(float value) { return insert(value); }
        TSeries & operator+(const TSeries & other)
        {
            if (this->_size != other._size)
            {
                std::cout << "Size doesn't match!" << std::endl;
            }
            else
            {
                //std::cout << "whee";
                for (int i = 0; i < this->_size; i++)
                {
                    _tab[i] += other._tab[i];
                    //std::cout << "nothing";
                }
            }
            //std::cout<<"sth";
            return *this;
        }
    
        TSeries& operator=(const TSeries& other)
        {
            _size = other._size;
            _capacity = other._capacity;
            //Create tmp in case of self-assignment
            float *tmp = new float[_capacity];
            memcpy(tmp, other._tab, _size*sizeof(float));
            delete[] _tab;
            _tab = tmp;
            return *this;
        }
    
        TSeries operator()(int position1, int position2)
        {
            //TODO: Range-Check
            return TSeries(_tab + position1, position2 - position1);
        }
    
        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];
            memcpy(tmp, _tab, _size*sizeof(float));
            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;
        return out;
    }
    
    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: " << series4 << endl;
    
        return 0;
    }
    
    /* output required:
    Series1: (1,2,4,2)
    Series2: (3,3,3,4,5,1,0)
    
    Size doesn't match!
    Series3: ()
    
    (1,2,4,2,1,0,3)
    +(3,3,3,4,5,1,0)
    ---------------------
    Series3:   (4,5,7,6,6,1,3)
    
    Series4: (4,2)
    */
    

    <强>更新

    您的Series1: (1,2,4,2) Series2: (3,3,3,4,5,1,0) Size doesn't match! Series3: (1,2,4,2) (4,5,7,6,6,1,3) +(3,3,3,4,5,1,0) --------------------- Series3: (4,5,7,6,6,1,3) Series4: (7,6) 应该看起来像这样:

    operator+(const TSeries &)

    你的TSeries operator+(const TSeries & other) { if (this->_size != other._size) { std::cout << "Size doesn't match!" << std::endl; return TSeries(); //Return empty object } TSeries tmp(*this); //Create copy for (int i = 0; i < tmp._size; i++) { tmp._tab[i] += other._tab[i]; } return tmp; } 就像这样:

    operator()(int, int)

    也许你想在错误情况下抛出异常?