繁体   English   中英

使用 class function C++ Visual Studio 调试断言失败错误

[英]Debug assertion failed error on using class function C++ Visual Studio

我是 C++ 的新用户

代码:

#include <iostream> 
#include <string> 
#include <fstream>

#include <windows.h>


class spawnTools {
    
private:
    void specTools(std::string toolname) {

    }
    void normTools(std::string toolname) {

    }
public:
    std::string toolList =
    { "regedit", "cmd" };
    std::string toolListAdmin =
    { "regedit", "cmd" };
    void initToolSet(int xadmin) {
        if (xadmin == TRUE) {

        }
        else if (xadmin == FALSE) {

        }
        else {
            MessageBox(NULL, L"SYSTEM ERROR: Incorrect input into xadmin", L"Error", MB_OK | MB_ICONERROR);
        }
    }
};

int main() {
    spawnTools st;
    st.initToolSet(TRUE); <----- Exception thrown here
}

我已经设置了一个 class,带有 2 个公共字符串和 1 个公共字符串 function。私有函数将在稍后的开发中填充(只是让你知道我没有隐藏任何代码)。

我收到一个调试断言错误,说我有一个转置指针范围; 这不像这个问题,因为我使用的是 std::strings,而不是向量字符。

无论如何,在使用公共 function 而不是 std::strings 时会抛出异常。

我曾尝试在 std::strings 上使用 ' 逗号而不是普通的 " 逗号,但这会引发另一个错误,因此它不起作用。

我试过使用struct以防它与class 没有运气。

您不能以这种方式用两个字符串初始化std::string C 字符串"regedit""cmd"被传递给一个构造函数,该构造函数通常用于传递要用来初始化它的字符串的开始和结束迭代器。 这导致它尝试使用"regedit"的地址作为字符串的起始迭代器(地址)和"cmd"的地址作为字符串的结束迭代器(地址)来初始化字符串。 您得到的断言是因为"cmd"的地址低于"regedit"

您可能需要一个std::array<std::string, 2>std::vector<std::string>或什至是一个裸露的std::string[2]数组来保存它们。

#include <string> 
#include <array>
#include <windows.h>

class spawnTools
{
private:
    void specTools(std::string toolname) {}
    void normTools(std::string toolname) {}

public:

    std::array<std::string, 2> toolList{ "regedit", "cmd" };
    std::array<std::string, 2> toolListAdmin{ "regedit", "cmd" };

    void initToolSet(int xadmin) {
        if (xadmin == TRUE) {}
        else if (xadmin == FALSE) {}
        else {
            MessageBox(
                NULL,
                L"SYSTEM ERROR: Incorrect input into xadmin",
                L"Error",
                MB_OK | MB_ICONERROR);
        }
    }
};

int main() {
    spawnTools st;
    st.initToolSet(TRUE);
}

暂无
暂无

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

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