解决 C++ 中潜在的漏报 C6385 错误

Resolving potential false negative C6385 Error in C++

这个问题很可能很常见,以前也有人问过。因此,在花了 30 分钟的时间试图找到我认为是假阴性的解决方案之后,我决定在这里发布我的问题。

我对 C++ 编码比较陌生,我想我会创建一个简单的随机项目生成器。不幸的是,在获取数组中的随机索引时,出现了 c6385 错误。此错误通常与我发现的研究中的无效或超载缓冲区有关。我相信这意味着由于 rand(),我尝试访问的索引太大了。

我会继续寻找解决方案,但希望了解其他人对这种情况的看法。我可能忽略了一个小细节或未能掌握一个概念。非常感谢所有帮助。如果我找到解决方案,我会 lock/remove 使论坛人满为患。

这是我的代码: ItemGeneration.h

#pragma once
#include <random>
#include <iostream>
using namespace std;

class ItemGeneration
{
public:
    /*
        Creating the cosntant variables for the items main effects.
        For instance, if you were to roll the main attribute of "Item1",
        this code would dictate what possible attributes that could be chosen.

        In addition to the main attributes, the code for the avaliable sub-attributes is also created below.

        Also creating the const array that hold the rarity of items.
        For instance, an item that rolls a higher weighted value will have a lower rating.
        More rare or 'uncommon' the value, the higher rating the item will recieve.
        Item rarity dictates the value of the attributes assigned to ie, i.e the higher the
        rarity the higher the attributes multiplier in addition to the number of sub-attributes.
    */

    const char* Item1[4]
        = { "Attack %", "Health %", "Defense %", "Elemental Damage %" };

    const char* Item2[4]
        = { "Mana Regeneration", "Cooldown Reduction", "Healing Efficacy", "Elemental Resonance" };

    const char* Item3[4]
        = { "Raw Attack", "Raw Health", "Raw Defense", "Raw Elemental Damage" };

    const char* Item4[4]
        = { "Elemental Resonance", "Critical Chance", "Critical Multiplier", "Mana Regeneration" };

    const char* Item5[4]
        = { "Elemental Damage %", "Critial Chance", "Critical Multiplier", "Cooldown Reduction" };

    const char* SubAttributeList[10]
        = { "Raw Attack", "Raw Health", "Raw Defense", "Raw Elemental Damage", "Elemental Damage %",
            "Elemental Resonance", "Mana Regeneration", "Cooldown Reduction", "Critical Chance", "Critical Multiplier" };

    const char* RarityChart[6]
        = { "Common", "Uncommon", "Rare", "Unique", "Legendary", "Mythical" };

    const int InventoryLimit = 256;
    int InventorySize = 0;

    int ItemCreate(int x, int y) {

        int randNum = rand() % 4 + 1;

        if (InventorySize < InventoryLimit) {
            switch (x) {
            case 1:
                cout << "You have generated an Item1! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item1[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 2:
                cout << "You have generated an Item2! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item2[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 3:
                cout << "You have generated an Item3! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item3[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 4:
                cout << "You have generated an Item4! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item4[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 5:
                cout << "You have generated an Item5! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item5[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            default:
                cout << "They item you tried to generate doesn't seem to exist.\nPlease enter a number between 1 and 5 to generate a random item." << endl;

            }
        }
        else {
            cout << "Sorry, your inventory is too full\nPlease clear some space before attempting to create another item." << endl;
        }
    }
};

Source.cpp

#include <iostream>
#include "ItemGeneration.h"
using namespace std;

int main() {
    int x, y;

    cout << "Welcome to the random item generator!" <<
        "\nPlease enter a number between 1 and 5, " <<
        "\nfollowed by a number between 1 and 10 to select its rarity, " <<
        "\nto receive your own randomly generated item.\n" << endl;

    cin >> x;
    cin >> y;

    ItemGeneration item;
    item.ItemCreate(x, y);
    return;
}

** 更新有错误 **

我应该更简洁一些。但是,我不认为存在实际的缓冲区错误,即漏报。 至于错误信息。

我的主要功能正在返回一个 c2561 错误,但我认为这是项目生成的副作用,无法正常运行。意思是它只是在函数未运行时返回值。 这是我尝试构建解决方案时的终端读数:

Build started...
1>------ Build started: Project: RandomItemGenerator, Configuration: Debug x64 ------
1>ItemGeneration.cpp
1>Source.cpp
1>C:\Users\Alex\source\repos\RandomItemGenerator\RandomItemGenerator\Source.cpp(18,2): error C2561: 'main': function must return a value
1>C:\Users\Alex\source\repos\RandomItemGenerator\RandomItemGenerator\Source.cpp(5): message : see declaration of 'main'
1>Generating Code...
1>Done building project "RandomItemGenerator.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

快速解决错误说明:

(const char [17]) "Main Attribute: "
C6385: Reding Invalid Data from 'this->item5'.
int main() {
   // stuff
   return;  // <- always an error
}

这是你的问题。你承诺 main 会 return 一个整数,然后你只用一个普通的 return 违背了这个承诺。 main 有点特殊,因为它应该 return 是一个整数,所以,如果其他都失败,请将其设置为 return 0.

int main() {
   // stuff
   return 0; 
}

您在 ItemGeneration::ItemCreate

中又做了一次
int ItemCreate(int x, int y) 
{
   // stuff
   return;    // nope
}

但在这种情况下,return 真的没有任何意义。您的 main 例程无论如何都会忽略 return 值。所以你应该将它声明为 return void 而不是:

void ItemCreate(int x, int y) 
{
   // stuff
   return;    // OK now (not actually needed, however)
}