简体   繁体   中英

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:

#define self_energy -2.0 * alpha / sqrtpi;

Then I got an error: expression result is unused for the variable 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.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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