#define出现“声明不声明任何内容”错误

时间:2019-07-19 15:46:19

标签: c++ compiler-errors macros g++ c++14

我试图将ll定义为long long的别名。但是,它没有编译并引发错误。

我在Windows计算机上使用VS Code。我也在使用gcc版本8.2.0。

这是代码-

#include <bits/stdc++.h>

using namespace std;

#define ll long long int;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;

    cin >> t;

    return 0;
}

编译时,出现此错误-

test.cpp: In function 'int main()':
test.cpp:5:22: error: declaration does not declare anything [-fpermissive]
 #define ll long long int;
                      ^~~
test.cpp:12:5: note: in expansion of macro 'll'
     ll t;
     ^~
test.cpp:12:8: error: 't' was not declared in this scope
     ll t;
        ^
test.cpp:12:8: note: suggested alternative: 'tm'
     ll t;

奇怪的是,这个确切的代码可以在其他机器上使用。有人可以向我解释一下吗?

1 个答案:

答案 0 :(得分:3)

预处理器指令后没有分号。

所以这个:

#define ll long long int;

意味着ll实际上是long long int;

然后您的声明:

ll t;

是真的:

long long int; t;

与以下相同:

long long int;
t;

希望现在您可以看到编译器讨厌它的原因。


顺便说一句,我意识到您正在执行“竞争性编程 [sic] ”,并且在该领域中,使所有内容简短且难以理解是很时髦的,但是像这样的宏确实是如果您想编写任何接近体面的代码,则避免使用。同样,do not include implementation headers

相关问题