2012-11-13 3 views
2

이 코드를 GNU C++ 컴파일러 (g ++)로 컴파일하려고하는데 작동하지 않는 것 같습니다. 저는 Vis Studio와 Code :: Blocks를 사용했으며 잘 작동합니다. 나는 컴파일러가 어떤면에서 다른 점을 알고 있으며 누군가 내 오류를 찾도록 도울 수 있는지 궁금해하고있다.GNU C++ 컴파일러의 세그먼트 오류

#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; 
} 
+2

? 컴파일 중 또는 런타임 중에? 정확히 어떤 오류가 있습니까? – Xymostech

+0

과 "Linux 컴파일러"는 무엇입니까? – iabdalkader

+0

@mux 제목에 "g ++"라고 말합니다. – Xymostech

답변

1

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; 
} 

무엇 other 배열이 적은 요소가 있다면? 정의되지 않은 동작이 발생합니다 (세분화 오류 포함)

3) m_array[i] += other.getValue(i);을 왜 사용하셨습니까? m_array은 비공개이므로? 액세스는 객체 수준이 아닌 클래스 수준에서 정의되므로 m_array[i] = other.m_arry[i]도 사용할 수 있습니다.

4) 나는 당신이 배열 클래스를 사용하는 코드를 게시 할 경우에만이 a good C++ book

5)는 segfault의 정확한 이유를 확인할 수 있습니다 읽어야하는 것이 좋습니다.

+0

예, 동의해야합니다.그러나 오류가 << 오버로드 된 연산자에서 발생하는 것으로 인해 발생한다고 생각합니다. 내가 테스트 코드를 사용하여 충돌 직전에 어떤 일이 일어나는지 확인합니다. – user1821626

+0

@ user1821626 : 컴파일러 오류 일 것입니다. 당신은 세분화를 얻고 있다고 말하고 있습니다 –

1

당신의 경고를 켜고 :

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 &

  1. 를, 그렇지 않으면 배열이 복사됩니다 :

    ,210

    인라인 친구 ostream에 & 운영자 < < (ostream에 & 다른, const를 배열 & 편곡)

  2. 초기화 포인터가없는 생성자에서 대입 연산자를 사용하지 마십시오

    배열 : 배열 (배열 다른 &) { * 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; 
} 
+0

당신이 옳았어요! 내가 코드 작성을 할 때 그것을 간과 한 것 같습니다. 도와 줘서 고마워. – user1821626

+0

@ user1821626 이것이 문제를 해결하면 [내 대답 수락] (http://meta.stackexchange.com/a/5235)을 고려하십시오. 고맙습니다. –