Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GNU C和x2B中的段故障+;编译程序 我试图让这个代码在GNU C++编译器(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; } #包括 使用名称空间std; 模板 类数组 { 私人: T*m_阵列; 国际货币单位大小; 公众: 数组(); 阵列(阵列和其他); 数组(整数大小); ~Array(); void设置值(int索引,T val); T getValue(int索引); int getSize(); 数组和运算符=(数组和其他); 数组和运算符+(数组和其他); 数组和运算符+(T val); 内联朋友ostream&operator_C++_Arrays_Templates_G++ - Fatal编程技术网

GNU C和x2B中的段故障+;编译程序 我试图让这个代码在GNU C++编译器(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; } #包括 使用名称空间std; 模板 类数组 { 私人: T*m_阵列; 国际货币单位大小; 公众: 数组(); 阵列(阵列和其他); 数组(整数大小); ~Array(); void设置值(int索引,T val); T getValue(int索引); int getSize(); 数组和运算符=(数组和其他); 数组和运算符+(数组和其他); 数组和运算符+(T val); 内联朋友ostream&operator

GNU C和x2B中的段故障+;编译程序 我试图让这个代码在GNU C++编译器(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; } #包括 使用名称空间std; 模板 类数组 { 私人: T*m_阵列; 国际货币单位大小; 公众: 数组(); 阵列(阵列和其他); 数组(整数大小); ~Array(); void设置值(int索引,T val); T getValue(int索引); int getSize(); 数组和运算符=(数组和其他); 数组和运算符+(数组和其他); 数组和运算符+(T val); 内联朋友ostream&operator,c++,arrays,templates,g++,C++,Arrays,Templates,G++,1)您应该真正了解const的正确性 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; } 模板 数组和数组::运

1)您应该真正了解const的正确性

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;
}
模板
数组和数组::运算符+(数组和其他)
{
对于(int i=0;i
如果
other
数组的元素较少怎么办?您将有未定义的行为(可能包括分段错误)

3) 为什么要使用
m_数组[i]+=other.getValue(i);
?因为
m_数组
是私有的?记住,访问是在类级别定义的,而不是对象级别,所以
m_数组[i]=other.m_数组[i]
也可以工作

4) 我建议你读一读

5) 只有在发布使用数组类的代码时,才能确定segfault的确切原因。

打开警告:

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
g++-std=c++0x-pedantic-Wall-Werror-gx.cc-ox
cc1plus:警告被视为错误
x、 cc:在函数“std::ostream&operator”中,我看到两个问题:

  • 使用
    常量&
    ,否则将复制数组:


    inline friend ostream&Operator您从哪里得到错误?在编译期间还是在运行期间?您到底得到了什么错误?@mux他们在标题中说“g++”。您为什么要编写自己的数组?(除了家庭作业之外)我阅读你的问题的方式听起来好像编译器正在崩溃,但是
    gcc
    使用非常频繁,而且通常很难崩溃。是的,我同意这应该得到修复。但是我相信这个错误是由@user1821626中发生的某件事导致的:这将是一个编译器错误。你是说你得到了一个分段是正确的!当我写代码时我一定忽略了。非常感谢您的帮助,先生。@ USS1821626,如果这解决了您的问题,请考虑。谢谢。
    inline friend ostream &operator<<(ostream &other, Array<T> arr)
    
    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;
    }