Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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++,我尝试使用复合语句,如下所示: namespace A { namespace B { { //compound statement started here void foo() { printf("%d\n",i); } int i=5; } //ended here } } int main(){ } 但

我尝试使用复合语句,如下所示:

namespace A
{
    namespace B
    {
        { //compound statement started here
            void foo()
            {
                printf("%d\n",i);
            }
            int i=5;
        } //ended here
    }
}

int main(){ }
但我有一个编译时错误:

test.cpp:7:3: error: expected unqualified-id before ‘{’ token
test.cpp:19:1: error: expected ‘}’ at end of input
test.cpp:19:1: error: expected ‘}’ at end of input
但我希望它是好的。这是因为复合语句定义为:

compound-statement:
   { statement-seq_opt }

在6.3/1中。

复合语句只允许在函数或方法中使用


如果我记得清楚的话,这是语法中语句和函数体“fct body”的一部分的特例。

名称空间中没有直接的语句,更不用说复合语句了

您只能在那里进行声明。您可以声明包含语句的函数,也可以使用带有初始值设定项的声明,它们可以像语句一样执行操作

在您的示例中,复合语句中的“语句”是声明,因此不清楚您希望额外的大括号起什么作用


请注意,函数定义(如示例
foo
)不是有效的声明语句。

标准中在哪里规定了它?请给我一个参考。@St.Antario如果您可以查找参考,您可以自己查找名称空间的语法。从语法生成索引开始,单击“命名空间体”。声明是C++中的语句。ITYM非声明语句。@MattMcNabb语句上下文中的声明就是语句。其他声明不适用。尽管如此,我认为这取决于你对BNF的理解有多建设性。