错误 C2143:语法错误:':'之前缺少';'
error C2143: syntax error : missing ';' before ':'
我收到错误:错误 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);
}
相关文章:
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 错误 C2143:语法错误:':'之前缺少','
- 编译错误 C2143:语法错误:'<'之前缺少';'
- 错误 C2143:语法错误:'<' C++之前缺少';'
- 错误 C2143:语法错误:在"*"之前缺少"{"
- 请求分号时出现令人困惑的错误(错误 C2143)
- 两个语法错误:错误 C2143 语法错误:'*'之前缺少';'
- 可变参数模板 - 错误 C2143:语法错误:'...'之前缺少','
- 错误 C2143:语法错误:':'之前缺少';'
- CS106b 库矢量.cpp错误 C2143:语法错误:'<'之前缺少';'
- 模板类引发错误 C2143:语法错误:在"<"之前缺少";"
- C++ 结构 - 错误 1 错误 C2143:语法错误:缺少'*'之前的';'
- 错误 C4430:AND 错误 C2143:语法错误:'*'之前缺少';'
- 错误 C2143:语法错误:声明指针时'*'之前缺少';'
- afxtempl.h 错误 12 错误 C2143:语法错误:缺少'}'之前的';'
- C++:错误 C2143:语法错误:'<'之前缺少';'
- 错误 C2143:语法错误:命名空间之前缺少";"
- 错误 C2143:语法错误:'if'之前缺少';'
- 错误 C2143:语法错误:在"__stdcall"之前缺少";"
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'