gpt4 book ai didi

c++ - 由于嵌套的匿名 block ,代码无法编译?

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:17 25 4
gpt4 key购买 nike

(摘自 Accelerated C++)

为什么下面的代码不起作用:

#include <iostream>
#include <string>

int main() {
{
const std::string s = "a string";
std::cout << s << std::endl;

{
const std::string s = "another string";
std::cout << s << std::endl;
}
}
}

我不允许在 C++ 中嵌套匿名 block 吗?尝试编译源代码时,gcc 给出了以下错误(我无法理解):

hello: In function `_start':(.text+0x0): multiple definition of`_start'/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.text+0x0):first defined herehello:(.rodata+0x0): multipledefinition of `_fp_hw'/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata+0x0):first defined here hello: In function`_fini': (.fini+0x0): multipledefinition of `_fini'/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0):first defined herehello:(.rodata+0x4): multipledefinition of `_IO_stdin_used'/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata.cst4+0x0):first defined here hello: In function`__data_start': (.data+0x0): multipledefinition of `__data_start'/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.data+0x0):first defined here hello: In function`__data_start': (.data+0x4): multipledefinition of `__dso_handle'/usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o:(.data+0x0):first defined here hello: In function`_init': (.init+0x0): multipledefinition of `_init'/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0):first defined here /tmp/cchh83A6.o: Infunction `main':hello.cpp:(.text+0x0): multipledefinition of `main'hello:(.text+0xb4): first defined here/usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o:(.dtors+0x0):multiple definition of `__DTOR_END__'hello:(.dtors+0x4): first defined here/usr/bin/ld: warning: Cannot create.eh_frame_hdr section, --eh-frame-hdrignored. /usr/bin/ld: error inhello(.eh_frame); no .eh_frame_hdrtable will be created. collect2: ldreturned 1 exit status

最佳答案

该代码工作正常。看这里Your code compiled (你的环境有问题)

关于c++ - 由于嵌套的匿名 block ,代码无法编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4239686/

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