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

error C2143: syntax error : missing ';' before ':'

本文关键字:错误 C2143 语法      更新时间:2023-10-16

我收到错误:错误 C2143:语法错误:在 Visual Studio 中缺少 ':" 之前缺少 ';'

对于以下 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;
    }
}

; 之后 #define 不得存在。

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

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

您的#define末尾有分号。 因此,一旦它们被扩展,您就有:

    switch(key){
        case '1';:

去掉分号。 #define不需要分号;当线路结束时,它们结束。

你可以阅读这个。


#define后不要使用;。例如,当您要初始化变量时,应使用此方法:

#define Max_number 10000

潜在问题:

#define Max_number 10000;    // this is an error
#define Max_number = 10000   // this is also an error

有时您可以将其用作函数:

#include <iostream>
#define show(x) cout << #x << " is : " << x;
int main(){
    int number = 76;
    std:: show(number);
}