0

I have defined a function not in class

#define BlendLight(b1, b2) std::max(b1, b2) 

then in the class I am trying to use it:

float someFunk(float x, float y) { return BlendLight(x,y); //Error here - BlendLight marked red ( } 

And I get Error: Expected an identifier

I am try to compile this in Visual Studio 2010 std::max() header is included / I have add algorithm but error still present (((

5
  • Is the compiler that picky, too? Commented Jan 23, 2012 at 11:17
  • Is the header file with std::max() included? Commented Jan 23, 2012 at 11:18
  • Is BlendLight defined in the same file as someFunk? If BlendLight is in a header file, do you include that header file? Commented Jan 23, 2012 at 11:21
  • If you replace return BlendLight(x,y); with return std::max(x,y); does the compiler complain? Commented Jan 23, 2012 at 11:27
  • above code is working fine on my system. Better if you can elaborate your problem. Commented Jan 23, 2012 at 11:31

4 Answers 4

3

The code isn't erroneous as it stands. Most likely you have forgotten to

#include <algorithm> 

This is the header file where std::max is defined.

Another possibility is that you didn't define BlendLight in the same file as the class where you want to use it. In that case you have to #include the header file where BlendLight is defined.

Apart from this, you should know that what you have defined is not a function, but a preprocessor macro. In C++, you should rather use a proper (maybe inline) function for this task, so that your compiler can do type checking:

#include <algorithm> // ... template <class T> T BlendLight(T x, T y) { return std::max(x, y); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Nicklas. I am trying to "translate" an objective c project to c++ thath why I am doing the code close to original. Macros still not working (( even if I wrote <algorithm>. Perhaps I ll use your solution but as I suppose the macros in such signature could compare any numerical types not only float
@Papa: C++ has templates, this invalidates most use cases for macros. I updated the answer to allow any data type. As for your original problem: Did you define BlendLight in the same file where it is used? And, does replacing BlendLight with std::max in someFunk also give an error?
ohh.. you right. when I have change it to std::max it gived me error(. But I have add #include <algorithm> !
@Papa: Do you #include <algorithm> in the same source file where std::max is used and it yields an error? Which error?
Sorry, I have make a primitive mistake.. all workes like a charm. Thank you once more Niklas !
3

Have you forgotten to add /DNOMINMAX to your command line when invoking the compiler. Historically, VS has defined min and max as macros (and suppressed their definition in the standard header), in <windows.h>, I think, and requires the above option in order to be standards compliant. (I've not checked whether this is still the case in VS 2010, since I always define this systematically.)

Comments

0

You should better not be using macros for such definitions. It makes you horrible time when debugging it after some weeks.

Anyhow, have you included ?

2 Comments

agree)) but see my comment for Niklas Baumstark answer
@PapaJohn I see your point. Anyhow, if you agree with my posts please add me some reputation.
0

I think you should include algorithm before using BlendLight(b1, b2) because you used max which is available in algorithm library.

1 Comment

No, he should include <algorithm> before using BlendLight.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.