Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 由于嵌套匿名块,代码未编译?_C++ - Fatal编程技术网

C++ 由于嵌套匿名块,代码未编译?

C++ 由于嵌套匿名块,代码未编译?,c++,C++,取自加速C++ 为什么以下代码不起作用: #include <iostream> #include <string> int main() { { const std::string s = "a string"; std::cout << s << std::endl; { const std::string s = "another string";

取自加速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++中嵌套匿名块吗?当试图编译源代码时,gcc给了我以下错误,我无法理解:


这个代码很好用。请参见此处,您的环境有问题,代码运行正常。请参见此处,您的环境有问题

您似乎正在链接2个或更多定义相同内容的对象文件或.a文件。如果您链接到一个或另一个,您的链接器问题将消失。

您似乎链接到2个或更多定义相同内容的对象文件或.a文件。如果你链接一个或另一个链接,你的链接器问题就会消失。

你用什么命令来编译和链接这个链接?另外,如果删除匿名块,是否仍有错误?错误似乎出现在生成环境中,而不是代码中。请发布一些有关IDE和生成设置文件的信息?生成的building命令可能会在发布输出之前的控制台中看到。命名的非匿名块会是什么样子您使用什么命令来编译和链接此文件?另外,如果删除匿名块,是否仍有错误?错误似乎出现在生成环境中,而不是代码中。请发布一些有关IDE和生成设置文件的信息?生成的building命令可能会在发布输出之前的控制台中看到。命名的非匿名块会是什么样子哦。你说得对,这就是问题所在。它没有编写g++-o hello.cpp,而是编写了g++hello.cpp.D'oh。你说得对,这就是问题所在。它没有编写g++-o hello.cpp,而是编写了g++hello.cpp。 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 here hello:(.rodata+0x0): multiple definition 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): multiple definition of `_fini' /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0): first defined here hello:(.rodata+0x4): multiple definition 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): multiple definition 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): multiple definition 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): multiple definition of `_init' /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0): first defined here /tmp/cchh83A6.o: In function `main': hello.cpp:(.text+0x0): multiple definition 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-hdr ignored. /usr/bin/ld: error in hello(.eh_frame); no .eh_frame_hdr table will be created. collect2: ld returned 1 exit status