VS2013编译器错误,函数调用中带有#define符号 [英] VS2013 compiler error with a #define symbol in a function call
问题描述
在以下代码中:
In following code:
#define VMIN -210.0f
#define VMAX 220.0f
#define coef_a (0.5f*(VMIN+VMAX))
#define coef_b_green (510.0f/(VMAX-VMIN))
#define coef_b_red (510.0f/(VMIN-VMAX))
//#define LIMITUCHAR(a) ((a) < (255) ? (a) : 255)
uchar limit_uchar(float x)
{
if (x<255.5f)
{
if (x<0) return (uchar) 0;
else return (uchar) x;
}
else return (uchar) 255;
}
/*
Generates a color as a function of v: if v<vmed, assign red, if v>vmed assign green
At the middle color=black
*/
void assigncolor(float v,uchar color[3])
{
color[0]=0;
color[1]=limit_uchar((v-coef_a)*coef_b_green);
color[2]=limit_uchar((v-coef_a)*coef_b_red);
}
以下行生成编译错误 coef_b_green 必须是可修改的左值
The following line generates compilation error due coef_b_green must be a modifiable lvalue
color[1]=limit_uchar((v-coef_a)*coef_b_green);
如何避免它?
< b>我尝试了什么:
这没关系。 coef_b_red 不会产生问题:
How can I avoid it?
What I have tried:
This is OK. coef_b_red does not create problem:
void assigncolor(float v,uchar color[3])
{
color[0]=0;
color[1]=limit_uchar((v-coef_a)*510.0f/(VMAX-VMIN));
color[2]=limit_uchar((v-coef_a)*coef_b_red);
}
我也试过不同的braquet等级
I also tried different braquet levels
推荐答案
以下代码
The following code
#include <iostream>
using namespace std;
typedef unsigned char uchar;
#define VMIN -210.0f
#define VMAX 220.0f
#define coef_a (0.5f*(VMIN+VMAX))
#define coef_b_green (510.0f/(VMAX-VMIN))
#define coef_b_red (510.0f/(VMIN-VMAX))
//#define LIMITUCHAR(a) ((a) < (255) ? (a) : 255)
uchar limit_uchar(float x)
{
if (x<255.5f)
{
if (x<0) return (uchar) 0;
else return (uchar) x;
}
else return (uchar) 255;
}
/*
Generates a color as a function of v: if v<vmed, assign red, if v>vmed assign green
At the middle color=black
*/
void assigncolor(float v,uchar color[3])
{
color[0]=0;
color[1]=limit_uchar((v-coef_a)*coef_b_green);
color[2]=limit_uchar((v-coef_a)*coef_b_red);
}
int main()
{
float v = 0.5;
uchar c[3];
assigncolor(v, c);
for (const auto & x : c)
cout << static_cast<unsigned int>(x) << endl;
}
在我的Linux机器箱中编译并生成一些输出。
请注意, C ++
为 #define
提供了更好的替代方案,例如,你可以写一下
Compiles and produces some output in my Linux box.
Please note, C++
offers better alternatives to #define
, for instance, you might write
constexpr float VMIN = -210.0f;
constexpr float VMAX = 220.0f;
constexpr float coef_a = (0.5f*(VMIN+VMAX));
constexpr float coef_b_green = (510.0f/(VMAX-VMIN));
constexpr float coef_b_red = (510.0f/(VMIN-VMAX));
CPallini absolut正确,但我会更进一步:#define是为了避免WHERE EVER成为可能,因为它使代码和编译器(以及链接器和优化)的可读性变得复杂。
我的编译器使用上面的编译器码。我猜你有一些拼写错误(在你的原始代码中看起来像隐形字符)或者VS 2013有一些问题。
但是真的:避免#define因为它会杀死一些代码处理逻辑,它将帮助您编写更好的代码,以便更好的应用程序
想想代码流程从255到。 255.4999。喜欢:
CPallini is absolut correct, but I would go a big step further: #define are to avoid WHERE EVER possible because it complicates the readability of code AND the compiler (and so linker and optimization).
My compiler did compile with the above code. I guess that you have some typo (in your original code like invisible chars) or VS 2013 has some issues.
But really: avoid #define because that kills some code processing logic which will help you to write better code and so a better app
Think about the code flow in the range from 255. to 255.4999. Like:
limit_uchar(255.3);
根据您期望输入的方式,该功能可能会略微平滑
Depending how you "expect" your input the function may run a slight smoother
uchar limit_uchar(float x)
{
if (x>255.) {
return (uchar) 255
}
if (x<0) {
return (uchar) 0;
}
return (uchar) x;
}
将常见案例置于最顶层。
By bringing the common cases at the top.
我通过更改解决了问题:
I solved the problem by changing:
#define VMIN -210.0f
By:
By:
#define VMIN (-210.0f)
问题发生在VS编译器行:
The problem was at the VS compiler line:
#define coef_b_green (510.0f/(VMAX-VMIN))
编译器不懂双 -
The compiler do not understand double --
#define coef_b_green (510.0f/(VMAX--210.0f))
注意:我接受了解决方案-1但使用了const:
NOTE: I accepted the solution-1 but using const:
const float VMIN =-210.0f;
const float VMAX =(220.0f);
const float coef_a= (0.5f*(VMIN+VMAX));
const float coef_b= (510.0f/(VMAX-VMIN));
只要未接受constexpr我的VS2013编译器。
结果它使用 #define 从400 megaoperationss /秒增加 assigncolor()功能的速度到454 MOPS使用 const float
不幸的是,如果你使用C或openCL,你应该使用#define但设置负值在括号内
As long as constexpr was not accepted by my VS2013 compiler.
As result it was increased the speed of assigncolor() function from 400 megaoperationss/second using #define to 454 MOPS using const float
unfortunalely if you arte using C or openCL you should use #define but setting negative values inside parentheses
这篇关于VS2013编译器错误,函数调用中带有#define符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!