简体   繁体   English

C ++中出现错误“表达式结果未使用”

[英]error “expression result is unused” in c++

I have some code like: 我有一些类似的代码:

double calculate_self_term(double area)     {
    double corr = 2.0 * sqrtpi / sqrt(area);
    return calculate_reciprocal(0, 0, 0) + self_energy + corr;
}

where self_energy is defined at the beginning: self_energy处定义了self_energy地方:

#define self_energy -2.0 * alpha / sqrtpi;

Then I got an error: expression result is unused for the variable corr . 然后我得到一个错误:变量corr 表达式结果未使用

Don't use macros for such things. 不要将宏用于此类操作。

With your definition: 根据您的定义:

#define self_energy -2.0 * alpha / sqrtpi;

Your code substitutes to: 您的代码替代为:

return calculate_reciprocal(0, 0, 0) + -2.0 * alpha / sqrtpi; + corr;

At which point the issue should be obvious - you have this +corr expression statement, after your return statement, which is doing nothing. 在这一点上,问题应该很明显-在return语句之后,您有这个+corr表达式语句,它什么也没做。

If you didn't use the macro, you wouldn't have run into such an issue. 如果您不使用宏,则不会遇到这样的问题。 Don't use macros for such things. 不要将宏用于此类操作。 This should probably just be a function: 这可能只是一个函数:

constexpr auto self_energy(double alpha) { return -2.0 * alpha / sqrtpi; }
// ...
return calculate_reciprocal(0, 0, 0) + self_energy(alpha) + corr;

Remove the semicolon in the macro 删除宏中的分号

#define self_energy -2.0 * alpha / sqrtpi;
                                        ^^^

and write it like 像这样写

#define self_energy ( -2.0 * alpha / sqrtpi )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM