이 코드를 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;
}
? 컴파일 중 또는 런타임 중에? 정확히 어떤 오류가 있습니까? – Xymostech
과 "Linux 컴파일러"는 무엇입니까? – iabdalkader
@mux 제목에 "g ++"라고 말합니다. – Xymostech