[英]c++17 error message: “reference to non-static member function must be called” when using a macro on a vector
I'm learning c++ and was playing around with macros.我正在学习 c++ 并且正在玩宏。 I tried defining push_back as pub, and it gave me this error:我尝试将 push_back 定义为 pub,它给了我这个错误:
error: reference to non-static member function must be called
vect.pub(1);
Here's my code:这是我的代码:
#include <vector>
using namespace std;
typedef vector<int> vi;
#define pub push_back;
int main(){
vi vect;
vect.pub(1);
}
When I didn't use the #define
and just wrote push_back
, there was no error messages.当我不使用#define
并且只写push_back
时,没有错误消息。
What exactly changed when I used the macro?当我使用宏时到底发生了什么变化?
You should not put ';'你不应该放';' for macro.为宏。
#include <vector>
using namespace std;
typedef vector<int> vi;
#define pub push_back
int main(){
vi vect;
vect.pub(1);
}
#define pub push_back;
//...
vect.pub(1);
This expands to the following, which is invalid syntax due to the extra ;
这扩展为以下内容,由于额外的;
这是无效的语法. .
vect.push_back;(1);
So drop the ;
所以放弃;
and #define pub push_back
.和#define pub push_back
。
I'm learning c++ and was playing around with macros.我正在学习 c++ 并且正在玩宏。
Stop.停止。 push_back
is at most 6 extra keystrokes. push_back
最多 6 次额外击键。 Code is meant to be read by humans.代码是供人类阅读的。 You can't find pub
in documentation, but you can find push_back
.您在文档中找不到pub
,但可以找到push_back
。
Similarly using namespace std;
同样using namespace std;
is a terrible habit.是一个可怕的习惯。 There are loads of names that you don't realise you've just imported into the global namespace there.有很多您没有意识到您刚刚导入到全局命名空间的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.