如何使用具有 (this) 作为变量名的 .h 文件到 CPP 文件中

muk*_*rma 0 c++

我有一个标题 my.h,其中有一个名为 my_func 的函数的以下声明。

#pragma once
void my_func (int *this);
Run Code Online (Sandbox Code Playgroud)

这个函数是在my.c中实现的

void my_func (int *this)
{
  printf("%d", *this);
}
Run Code Online (Sandbox Code Playgroud)

在 foo.cpp 我想使用 my_func

extern "C"
{
    #include <foo.h>
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误错误:expected ')' void my_func (int *this); 实际上在我的项目中,在 .C/.h 文件中的很多地方使用/操作。在许多头文件/.c 文件中用作变量。为了简单起见,我只是采用了上面的示例来描述我面临的编译错误。

小智 8

this是C++ 中的关键字。您不能将其用作变量。您可以简单地将其重命名为标题中的其他名称。

事实上,您甚至可以完全删除变量名。编译器不会在意。命名参数用于告诉开发人员该参数要代表什么。它不会通知编译器任何事情。