(从加速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++?当试图编译源,海湾合作委员会给了我下面的错误(我不能作出任何意义):
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
你用什么命令来编译和链接它?另外,如果你删除了“匿名块”,你是否仍然有错误? – 2010-11-21 19:07:57
该错误似乎是在您的构建环境中,而不是在您的代码中。请发布一些关于您的IDE和构建设置(一个makefile?)的信息,并生成构建命令(您可能在发布之前在控制台中看到它)。 – Kos 2010-11-21 19:11:25
命名(非匿名)块的外观是什么? :) – fredoverflow 2010-11-21 19:26:58