刚刚将我的程序从 windows 移到了 linux,当从 main 调用 operator<< 函数时,现在工作的相同代码给了我一个分段错误。(概述)我的程序是一个 Vector 类,它接受输入并返回输入的内容,但是当达到它时它会崩溃,如果我从中<< endl
删除它不会崩溃?endl
main()
..///main
VecXd<int> x;
cout << "Input vector a\n";
cin >> a;
cout << "Test A: "<< a << endl; //seg fault, ->
cout << "Test A: " << a; //works
//----- class VecXd\\ opertor<< def + operator>>
/******************************************************/
friend istream &operator>>(istream &input, VecXd& vec)
{
for(int i = -1; i <= vec.dimension - 1; i++)
{
if(i == -1)
{
input >> vec.dimension;//>> (V vecArr = new V[vec.dimension]);
cout << vec.dimension << " dimension check" << endl;
vec.vecArr = new V[vec.dimension];
//vec.dimension = vecArr[0];
//cout << vec.dimension << " dimension check" << endl;
}
else
{
input >> vec.vecArr[i];//>> (V vecArr = new V[vec.dimension]);
cout << vec.vecArr[i] << " value check" << endl;
}
}
}
friend ostream& operator<<(ostream& output, const VecXd& vec)
{
for(int i = 0; i < vec.dimension; i++)
{
output << vec.vecArr[i] << " ";
}
output << endl;
// output << endl;
}
/****************************************************/
为什么endl会导致崩溃?数组结束输出 endl 不解决这个问题吗?