#define 的"Declaration does not declare anything"错误

"Declaration does not declare anything" error for #define

本文关键字:declare anything not 错误 does Declaration #define      更新时间:2023-10-16

我试图将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;

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

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

所以这个:

#define ll long long int;

意味着ll字面上是long long int;.

然后您的声明:

ll t;

真的是:

long long int; t;

这与

long long int;
t;

希望现在你能明白为什么你的编译器讨厌它。


顺便说一句,我意识到你正在做"竞争性编程[原文如此]",并且在这个领域让一切都简短和不可读是时髦的,但如果你想写任何接近体面的代码,真的要避免像这样的宏。同样,不要包含实现标头。