C ++:gcc 4.2.1给程序哪个段错误,而不是linux

时间:2011-12-29 20:42:38

标签: c++ segmentation-fault

以下简单的程序段错误在我的mac(Lion)上运行gcc 4.2.1:

#include <iostream>

using namespace std;
struct A{
  friend std::ostream& operator << (std::ostream& os, const A& a) {
    os << 3 << endl;
  }
};

template <typename T>
T f() { return T();}

int f() { return 2;}


int main() {
  cout << f() << endl;

  A a= f<A>();
  cout << a << endl;
}

当我运行程序时,我得到:

 ./a.out
2
3
Segmentation fault: 11

当我进行堆栈跟踪时,我得到:

(gdb) run

Starting program: a.out 
unable to read unknown load command 0x24
unable to read unknown load command 0x26
2
3

Program received signal SIGSEGV, Segmentation fault.
0x00007fff8b84fa49 in ?? ()
(gdb) bt
#0  0x00007fff8b84fa49 in ?? ()
#1  0x00007fff665c1ae8 in ?? ()
#2  0x0000000000000000 in ?? ()

回溯没有有用的信息(任何人都知道为什么?)。这有效 在linux中很好。

1 个答案:

答案 0 :(得分:6)

让我们更清楚发生的事情

(cout << a) << endl;

您忘记了return中的operator<<

相关问题