gpt4 book ai didi

C++:gcc 4.2.1 给出了段错误的程序,而不是在 linux 上

转载 作者:行者123 更新时间:2023-11-30 00:57:11 25 4
gpt4 key购买 nike

以下简单程序在我的 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 中很好。

最佳答案

让我们更清楚发生了什么

(cout << a) << endl;

您忘记了 return在你的operator<< .

关于C++:gcc 4.2.1 给出了段错误的程序,而不是在 linux 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8673195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com