自创模板矩阵类

时间:2018-01-09 15:27:21

标签: c++ templates matrix segmentation-fault sigsegv

我试图编写代码来实现带有边界检查和附加运算符的矩阵的模板。但是,我现在正陷入SIGGSEGV。 矩阵基于我自己写的矢量类:

#ifndef VEKTOR_H
#define VEKTOR_H
#include "Exeptions.h"
template<typename T>
class vektor
{  
    private:
        unsigned int length;
        T* items;

    public:
        inline unsigned int getLength() const{return length;};

        vektor():length(0),items(nullptr)
        {
            //ctor
        }

        virtual ~vektor()
        {
            delete [] items;
        }

        vektor(const vektor& other):length(other.getLength()),items(new T[length])
        {
            for(int i = 0;i<length;i++)
            {
                items[i]=other[i];
            }
        }

        vektor(int len):length(len),items(new T[len])
        {

        }

        vektor<T>& operator=(const vektor& rhs)
        {
            if (this == &rhs) return *this; // handle self assignment
            //assignment operator
            length = rhs.getLength();
            items = new T[length];
            for(int i = 0;i<length;i++)
            {
                items[i]=rhs[i];
            }
            return *this;
        }

        T& operator[](const unsigned int index)const
        {
            if(index >= 0 && index < length)
            {
                return items[index];
            }
            else throw out_of_bounds();
        }
};
#endif // VEKTOR_H

然后是矩阵的代码:

#ifndef MATRIX_H
#define MATRIX_H
#include "vektor.h"
template <typename T>
class matrix
{
    private:
        int columns;
        vektor<T>* tabs;

    public:
        inline int getColCount()const{return columns;};
        inline int getRowCount()const{return tabs[0].getLength();};
        inline vektor<T>* getTabs()const{return tabs;};

        matrix():columns(0),tabs(new vektor<T>(0))
        {
            //ctor
        }
        matrix(int columns, int rows):columns(columns),tabs(new vektor<T>[columns])
        {
            for(int i = 0; i< columns;i++)
            {
                tabs[i] = *new vektor<T>(rows);
            }
        }
        virtual ~matrix()
        {
            delete tabs;
        }
        matrix(const matrix& other):rows(other.getColCount()),tabs(new vektor<T>(*other.getTabs()))
        {
            //copy ctor
        }

        matrix<T>& operator=(const matrix& rhs)
        {
            if (this == &rhs) return *this; // handle self assignment
            //assignment operator
            spalten = rhs.getColCount();
            tabs = new vektor<T>(*rhs.getTabs());
            return *this;
        }

        vektor<T>& operator[](unsigned int index)
        {
            return tabs[index];
        }
};

#endif // MATRIX_H

一个包装类,用字符串实例化矩阵以创建类似菜单的结构:

MenUI::MenUI():selectedTab(3),selectedItem(4),menItems(matrix<string>(3,4))
{
    menItems[0][0] = "File";
    menItems[0][1] = "Edit";
    menItems[0][2] = "View";
    menItems[0][3] = "Search";
    menItems[1][0] = "New";
    menItems[1][1] = "Undo";
    menItems[1][2] = "Perspectives";
    menItems[1][3] = "Find";
    menItems[2][0] = "Open...";
    menItems[2][1] = "Redo";
    menItems[2][2] = "Toolbars";
    menItems[2][3] = "Find in Files";
}

此处SIGSEGV发生在第tmp = menItems[selectedTab][i];

void MenUI::print()
{
    int offset = 0;
    string tmp;
    for(unsigned int i = 0; i<menItems.getColCount();i++)
    {
        tmp = menItems[i][0];
        if (i == selectedTab) cout  << "|- " << setw(10) << tmp << " -";
        else cout << "|  " << setw(10) << tmp << "  ";
    }
    cout << "|" << endl;
    offset = selectedTab * (10 + 5);
    for(unsigned int i = 1;i<menItems.getRowCount();i++)
    {
        tmp = menItems[selectedTab][i];
        if(i == selectedItem) cout << string(offset-3, ' ') << "|> " << setw(10) << tmp << " <|" << endl;
        else cout << string(offset-3, ' ') << "|  " << setw(10) << tmp << "  |" << endl;
    }
}

由于这是我第一次使用模板,我现在有点迷失了。 我使用Code :: Blocks IDE和GCC。 如果有人能指出我正确的方向,那将是非常好的。 谢谢!

1 个答案:

答案 0 :(得分:0)

如果selectedTab为3并且您tmp = menItems[selectedTab][i];,则您的代码将因为您在内存边界之外访问而出错。

我可能已经按照字面意思写了你所写的内容而你改变了selectedTab