簡體   English   中英

GNU C++ 編譯器中的段錯誤

[英]Segment Fault in GNU C++ Compiler

我試圖讓這段代碼在 GNU C++ Compiler (g++) 中編譯,但它似乎不起作用。 我使用過 Vis Studio 和 Code::Blocks,它運行良好。 我知道編譯器在某些方面有所不同,並且想知道是否有人可以幫助我找到我的錯誤。

#include <iostream>
using namespace std;

template <class T>
class Array
{
    private:
        T *m_array;
        int m_size;
    public:
        Array();
        Array(Array& other);
        Array(int size);
        ~Array();
        void setValue(int index, T val);
        T getValue(int index);
        int getSize();
        Array &operator=(Array &other);
        Array &operator+(Array &other);
        Array &operator+(T val);
        inline friend ostream &operator<<(ostream &other, Array<T> arr)
        {
            for (int i = 0; i < arr.getSize(); i++)
            {
                other << arr.getValue(i) << " ";
            }
        }
};

template<class T>
Array<T>::Array()
{
    m_array =  NULL;
    m_size = 0;
}
template<class T>
Array<T>::Array(int size)
{
    m_size = size;
    m_array = new T[size];
}
template<class T>
Array<T>::Array(Array& other)
{
    *this = other;
}
template<class T>
Array<T>::~Array()
{
    delete[] m_array;
}
template<class T>
void Array<T>::setValue( int index, T val )
{
    m_array[index] = val;
}
template<class T>
T Array<T>::getValue( int index )
{
    return m_array[index];
}
template<class T>
Array<T> &Array<T>::operator=( Array& other )
{
    if (m_array != NULL)
        delete[] m_array;

    m_size = other.getSize();

    m_array = new T[m_size];

    for (int i = 0; i < other.getSize(); i++)
    {
        m_array[i] = other.getValue(i);
    }

    return *this;
}
template<class T>
Array<T> &Array<T>::operator+( Array &other )
{
    for (int i = 0; i < m_size; i++)
    {
        m_array[i] += other.getValue(i);
    }

    return *this;
}
template<class T>
Array<T> &Array<T>::operator+( T val )
{
    for (int i = 0; i < m_size; i++)
    {
        m_array[i] += val;
    }

    return *this;
}
template<class T>
int Array<T>::getSize()
{
    return m_size;
}

1)你應該真正了解常量正確性

2)這段代碼看起來很可疑

template<class T>
Array<T> &Array<T>::operator+( Array &other )
{
    for (int i = 0; i < m_size; i++)
    {
        m_array[i] += other.getValue(i);
    }

    return *this;
}

如果other Array 的元素較少怎么辦? 您將有未定義的行為(可能包括分段錯誤)

3) 你為什么用m_array[i] += other.getValue(i); ? 因為m_array是私有的? 請記住,訪問是在類級別而不是對象級別定義的,因此m_array[i] = other.m_arry[i]也可以工作。

4)我建議你應該讀一本好的C++書

5) 只有當您發布使用您的數組類的代碼時,才能確定段錯誤的確切原因。

打開警告:

g++ -std=c++0x -pedantic -Wall -Werror -g    x.cc   -o x
cc1plus: warnings being treated as errors
x.cc: In function ‘std::ostream& operator<<(std::ostream&, Array<T>)’:
x.cc:27: error: no return statement in function returning non-void

錯誤的功能是:

inline friend ostream &operator<<(ostream &other, Array<T> arr)

我看到兩個問題:

  1. 使用const & ,否則您的數組將被復制:

    內聯友元 ostream &operator<<(ostream &other, const Array &arr)

  2. 不要在沒有初始化指針的構造函數中使用賦值運算符:

    Array::Array(Array& other) { *this = other; }

這至少應該是:

Array<T>::Array(const Array& other)
    : m_array(0)
{
    *this = other;
}

我想這就是它崩潰的地方:

template<class T>
Array<T> &Array<T>::operator=( Array& other )
{
    if (m_array != NULL)
        delete[] m_array; // In copy constructor, deletes uninitialized pointer!

    m_size = other.getSize();

    m_array = new T[m_size];

    for (int i = 0; i < other.getSize(); i++)
    {
        m_array[i] = other.getValue(i);
    }

    return *this;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM