Uday
Uday

Reputation: 829

How to define a function with same name which is present in different file

I want to define a class like this

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

and implementing in cpp file

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

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

#define UpdateResource  UpdateResourceW

And hence while compiling i get following error

error C2039: 'UpdateResourceW' : is not a member of '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.

Thanks In Advance, Uday

Upvotes: 2

Views: 862

Answers (8)

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

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.

Upvotes: 5

user122302
user122302

Reputation: 132

Namespace is the way to go.

Upvotes: -2

James Hopkin
James Hopkin

Reputation: 13973

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).

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

#pragma pop_macro("UpdateResource")

Upvotes: 0

Patrick
Patrick

Reputation: 8310

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?

Upvotes: 1

heavyd
heavyd

Reputation: 17721

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

Upvotes: 0

Sasha
Sasha

Reputation:

Ugh, that is the evil of the preprocessor!

Upvotes: 0

Glen
Glen

Reputation: 22290

Macro's totally ignore scope.

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

renaming the method is your only option

Upvotes: 3

sharptooth
sharptooth

Reputation: 170499

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.

Upvotes: 1

Related Questions