为什么这段代码不起作用?如何使用cout函数显示代码? [英] Why doesn't this code work and how do I use cout function to display the code?

查看:107
本文介绍了为什么这段代码不起作用?如何使用cout函数显示代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

int main()
{
    int myArr[x];
    
    for (x=0;x<5;x++)
    {
        myArr[x]=42;
    }
    
    return 0;
}





我的尝试:





What I have tried:

$g++ -o main *.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:15: error: ‘x’ was not declared in this scope
     int myArr[x];
               ^
main.cpp:10:9: error: ‘myArr’ was not declared in this scope
         myArr[x]=42;

推荐答案

g ++ -o main * .cpp
main.cpp:在函数'int main()'中:
main.cpp:6:15:错误:'x'未在此范围内声明
int myArr [x];
^
main.cpp:10:9:错误:'myArr'未在此范围内声明
myArr [x] = 42;
g++ -o main *.cpp main.cpp: In function ‘int main()’: main.cpp:6:15: error: ‘x’ was not declared in this scope int myArr[x]; ^ main.cpp:10:9: error: ‘myArr’ was not declared in this scope myArr[x]=42;


Quote:

为什么这段代码不起作用?

Why doesn't this code work ?



As在错误消息中说


As said in error message

main.cpp:6:15: error: ‘x’ was not declared in this scope
     int myArr[x];
               ^




int main()
{
    int myArr[x];  // What the x on this line ? What is its value ?
                   // that is what the compiler is asking you.
                   // The line is declaring an array, but what is the size ?
    for (x=0;x<5;x++)
    {
        myArr[x]=42;
    }
    
    return 0;
}



您应该遵循几个教程来帮助您入门。


You should really follow a couple tutorials to get you started.


C++适合初学者 - Google搜索 [ ^ ]


这篇关于为什么这段代码不起作用?如何使用cout函数显示代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆