错误C2143:语法错误:缺少';'在':'之前

时间:2013-11-03 04:05:31

标签: c++ visual-c++

我收到错误:错误C2143:语法错误:缺少';'在视觉工作室中的':'之前

以下cpp代码。谁能解释为什么我会收到这个错误?

帮助表示感谢

#include<iostream>
using namespace std;

#define UP '1';
#define DOWN '2';
#define RIGHT '3';
#define LEFT '4';

void main()
{
    char key ;
    char value = 'x';
    cout<<"Enter 1 or 2 or 3 or 4"<< endl;
    cin>>key;
    switch(key)
    {
    case UP :
        cout<<"case UP"<<endl;
        break;
    case DOWN:
        cout<<"case DOWN"<<endl;
        break;
    case LEFT:
        cout<<"case LEFT"<<endl;
        break;
    case RIGHT:
        cout<<"case RIGHT"<<endl;
        break;
    }
}

4 个答案:

答案 0 :(得分:3)

#define之后一定不能出现。

答案 1 :(得分:2)

不要在定义语句之后放置;

    #define UP '1'
    #define DOWN '2'
    #define RIGHT '3'
    #define LEFT '4'

答案 2 :(得分:2)

你的#define在结尾处有分号。所以一旦他们扩展了,你就有了:

    switch(key){
        case '1';:

摆脱分号。 #define不需要分号;它们在线路结束时结束。

答案 3 :(得分:2)

您可以阅读this


请勿在{{1​​}}之后使用;。例如,当您要初始化变量时,应使用此方法:

#define

潜在问题:

#define Max_number 10000

有时您可以将其用作功能:

#define Max_number 10000;    // this is an error
#define Max_number = 10000   // this is also an error
相关问题