中为全局数组赋值

中为全局数组赋值

本文介绍了无法在C ++中为全局数组赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 #include <iostream>
    int tabela[1];
    tabela[0] = 1;
    int main(){
        std::cout << tabela[0];
        std::cin.get();
        return 0;
    }

,它不想工作.我的编译器说"tabela"没有命名类型".但是,如果我这样做:

and it doesn't want to work. My compiler says " "tabela" doesn't name a type".However if I do this:

#include <iostream>
int tabela[1];
int main(){
    tabela[0] = 1;
    std::cout << tabela[0];
    std::cin.get();
    return 0;
}

有效.某人能解释一下为什么吗?预先感谢.

It works. Can sb explain me why? Thanks in advance.

推荐答案

在最外层,C ++文件是一系列声明. tabela[0] = 1;不是声明-它是一条语句(特别是表达式语句).但是,函数主体是一系列语句,因此可以将此行放在main(或任何其他函数)的主体中.

At the outermost level, a C++ file is a sequence of declarations. tabela[0] = 1; is not a declaration - it's a statement (in particular an expression-statement). A function body, however, is a sequence of statements, so it's fine to put this line inside the body of main (or any other function).

这篇关于无法在C ++中为全局数组赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:36