如何定义具有相同名称的函数,该函数存在于不同的文件中

时间:2022-06-16 02:21:26

I want to define a class like this

我想定义一个这样的类

class HttpRestAccessor
{
public:
    IResource& UpdateResource(string& , IResource& );
};

and implementing in cpp file

并在cpp文件中实现

IResource& HttpRestAccessor::UpdateResource(string& resourceUri, IResource& resource)

this cpp file refers to Winbase.h which has defined UpdateResource as follows

这个cpp文件是指Winbase.h,它定义了UpdateResource,如下所示

#define UpdateResource  UpdateResourceW

And hence while compiling i get following error

因此在编译时我得到以下错误

error C2039: 'UpdateResourceW' : is not a member of 'RestToolkit::HttpRestAccessor'

错误C2039:'UpdateResourceW':不是'RestToolkit :: HttpRestAccessor'的成员

this problem would be solved if rename function name to something different. but I would love to keep my function name as UpdateResource.

如果将函数名重命名为不同的东西,这个问题就解决了。但我希望将我的函数名称保存为UpdateResource。

Thanks In Advance, Uday

在此先感谢Uday

8 个解决方案

#1


Macro's totally ignore scope.

Macro完全忽略了范围。

Basically the pre-precessor will do a find/replace of UpdateResource with UpdateResourceW so you're all out of luck.

基本上,前任前任将使用UpdateResourceW查找/替换UpdateResource,所以你们都运气不好。

renaming the method is your only option

重命名方法是您唯一的选择

#2


Just undefine it:

只是取消定义它:

 #undef UpdateResource

then redefine later if you actually need it.

如果你真的需要它,那么以后再重新定义。

EDIT: However, you should reconsider your aversion to renaming the method. As others have said, that's a more elegant solution.

编辑:但是,您应该重新考虑您对重命名方法的厌恶。正如其他人所说,这是一个更优雅的解决方案。

#3


The simplest way is to include winbase.h before all other headers (in stdafx.h) so that you don't care whether the method name is replaced.

最简单的方法是在所有其他头文件(在stdafx.h中)之前包含winbase.h,这样你就不必关心是否替换了方法名称。

#4


Rename your function. If you want to use windows libraries then you have to accept they take precedence, you'd never try to create a class called std::string would you?

重命名您的功能。如果你想使用windows库,那么你必须接受它们优先,你永远不会尝试创建一个名为std :: string的类吗?

#5


Ugh, that is the evil of the preprocessor!

呃,那是预处理器的邪恶!

#6


you could use the #undef preprocessor directive to undefine the UpdateResource macro before you declare you function.

在声明函数之前,可以使用#undef预处理程序指令取消定义UpdateResource宏。

#7


One way is to document that if Winbase.h is included, it must be included before this header. Normally that would be a horrible requirement, but with Windows headers, it's pretty much a given that you're in for a world of pain unless you include them first (normally in a precompiled header, as @sharptooth says).

一种方法是记录如果包含Winbase.h,则必须在此标头之前包含它。通常这将是一个可怕的要求,但是对于Windows标题,除非你首先包含它们(通常在预编译的头文件中,正如@sharptooth所说),否则它几乎是一个令人痛苦的世界。

That done, put this in your header:

完成后,将其放在标题中:

#ifdef UpdateResource
#pragma push_macro("UpdateResource")
#undef UpdateResource
#endif

Anyone simultaneously needing the original definition of UpdateResource and your class just needs to put

任何人同时需要UpdateResource的原始定义,你的类只需要放

#pragma pop_macro("UpdateResource")

#8


Namespace is the way to go.

命名空间是要走的路。

#1


Macro's totally ignore scope.

Macro完全忽略了范围。

Basically the pre-precessor will do a find/replace of UpdateResource with UpdateResourceW so you're all out of luck.

基本上,前任前任将使用UpdateResourceW查找/替换UpdateResource,所以你们都运气不好。

renaming the method is your only option

重命名方法是您唯一的选择

#2


Just undefine it:

只是取消定义它:

 #undef UpdateResource

then redefine later if you actually need it.

如果你真的需要它,那么以后再重新定义。

EDIT: However, you should reconsider your aversion to renaming the method. As others have said, that's a more elegant solution.

编辑:但是,您应该重新考虑您对重命名方法的厌恶。正如其他人所说,这是一个更优雅的解决方案。

#3


The simplest way is to include winbase.h before all other headers (in stdafx.h) so that you don't care whether the method name is replaced.

最简单的方法是在所有其他头文件(在stdafx.h中)之前包含winbase.h,这样你就不必关心是否替换了方法名称。

#4


Rename your function. If you want to use windows libraries then you have to accept they take precedence, you'd never try to create a class called std::string would you?

重命名您的功能。如果你想使用windows库,那么你必须接受它们优先,你永远不会尝试创建一个名为std :: string的类吗?

#5


Ugh, that is the evil of the preprocessor!

呃,那是预处理器的邪恶!

#6


you could use the #undef preprocessor directive to undefine the UpdateResource macro before you declare you function.

在声明函数之前,可以使用#undef预处理程序指令取消定义UpdateResource宏。

#7


One way is to document that if Winbase.h is included, it must be included before this header. Normally that would be a horrible requirement, but with Windows headers, it's pretty much a given that you're in for a world of pain unless you include them first (normally in a precompiled header, as @sharptooth says).

一种方法是记录如果包含Winbase.h,则必须在此标头之前包含它。通常这将是一个可怕的要求,但是对于Windows标题,除非你首先包含它们(通常在预编译的头文件中,正如@sharptooth所说),否则它几乎是一个令人痛苦的世界。

That done, put this in your header:

完成后,将其放在标题中:

#ifdef UpdateResource
#pragma push_macro("UpdateResource")
#undef UpdateResource
#endif

Anyone simultaneously needing the original definition of UpdateResource and your class just needs to put

任何人同时需要UpdateResource的原始定义,你的类只需要放

#pragma pop_macro("UpdateResource")

#8


Namespace is the way to go.

命名空间是要走的路。