You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

C++程序出现[Error] duplicate case value错误求解析与替代方案

Hey there! Let’s break down that duplicate case value error you’re running into with your C++ trigonometry program, and work through solutions that won’t mess with your existing code structure.

What’s causing this error?

In C++, every case label inside a switch statement must have a unique constant value. The compiler throws this error when it finds two or more case clauses using the same value—for example, case 90: appearing twice, or using macros/enums that accidentally evaluate to the same number for multiple cases.

Switch statements work by jumping directly to the matching label at runtime. If two labels share the same value, the compiler can’t figure out which code block to execute, so it flags this as a critical error.

Fixes that preserve your existing code structure

Since you can’t alter the overall structure of your code, here are two practical approaches:

1. Stack duplicate cases into a single branch

If the duplicate values correspond to angles that produce identical trigonometric results (like 0° and 360° for sine/cosine), you can stack the case labels together before the shared code block. This doesn’t change your code’s core structure—it just groups equivalent cases:

switch(angle) {
    case 0:
    case 360:  // Duplicate value now grouped with case 0
        result = sin(0.0);
        break;
    case 90:
        result = sin(M_PI / 2);
        break;
    // ... rest of your cases ...
}

This tells the compiler both values should trigger the same logic, eliminating the duplicate value conflict entirely.

2. Replace the switch with a lookup table (keep core logic intact)

If combining cases isn’t feasible (e.g., duplicate values are accidental but you can’t remove either case), you can swap the switch for a key-value lookup table. This keeps all your original trigonometry calculations untouched while avoiding the switch case error:

First, leave your existing calculation functions exactly as they are:

double calc_sin_0() { return sin(0.0); }
double calc_sin_90() { return sin(M_PI / 2); }
// ... all your other calculation functions ...

Then, replace the switch with a std::map (or array, if angles are contiguous) that maps angle values to your functions:

// Initialize the lookup table once (e.g., in main() or a constructor)
std::map<int, double(*)()> angle_calculators = {
    {0, calc_sin_0},
    {360, calc_sin_0},  // Duplicate value maps to the same function
    {90, calc_sin_90},
    // ... map other angles to their respective functions ...
};

// Look up and run the calculation
auto it = angle_calculators.find(angle);
if (it != angle_calculators.end()) {
    result = it->second();
} else {
    // Handle invalid angles (same as your original default case)
}

This approach maintains all your original logic and avoids the duplicate case issue by using a mapping instead of a switch statement.

内容的提问来源于stack exchange,提问作者Developer Here

火山引擎 最新活动