我无法解决以下错误 [英] I can not fix the following errors

查看:168
本文介绍了我无法解决以下错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdlib>

char x;
using namespace std;

char map[4][4];

map[1][1] = {x};

int main()
{
	return 0;
}



_____________________________________________________________________



编译时我收到以下错误:



错误1错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int



错误2错误C2369:'map':重新定义;不同的下标



错误3智能感知:此声明没有存储类或类型说明符



我是在Windows 7上的Microsoft Visual Studio 2012 Express上运行此操作


_____________________________________________________________________

When I compile I get the following errors :

Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Error 2 error C2369: 'map' : redefinition; different subscripts

Error 3 IntelliSense: this declaration has no storage class or type specifier

I'm running this on Microsoft Visual Studio 2012 Express on Windows 7

推荐答案

您只能在方法范围之外编写声明,因此在您编写时:



You can only write declarations outside of a method scope, so when you write:

map[1][1] = {x};





这是一个无效的陈述,因为它不是声明。为了使用它,将它放在main()方法中,如:





That is an invalid statement because it is not a declaration. In order to use that, put it inside the main() method, like:

int main()
{
    map[1][1] = x;
    return 0;
}





但是,由于你没有用任何值初始化x,它会是一些随机值,或者你可能得到关于使用未初始化变量的警告。



However, since you did not initialize x with any value, it will be some random value or you may get a warning about using an uninitialized variable.


尝试此代码:

Try this code:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdlib>

char x;
using namespace std;

char map[4][4];



int main()
{
	map[1][1] = { x };
	return 0;
}
</cstdlib></string></string.h></iostream></windows.h>



您只需将 map [1] [1] = {x}; 行移至 main()方法。原因是:您可以将变量声明放在函数外部,但如果您尝试更改此变量的值,则必须在函数内部。


You just need to move the map[1][1] = { x }; line into the main() method. The reason: you can put variable declarations outside functions, but if you try to change the value of this variable, then this has to be inside a function.


这篇关于我无法解决以下错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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