Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C6385和x9;从'读取无效数据;ss';:可读大小为';352';字节,但'-176';可以读取字节_C++_Visual C++ - Fatal编程技术网

C++ C6385和x9;从'读取无效数据;ss';:可读大小为';352';字节,但'-176';可以读取字节

C++ C6385和x9;从'读取无效数据;ss';:可读大小为';352';字节,但'-176';可以读取字节,c++,visual-c++,C++,Visual C++,我在VisualStudio中从事OpenGL项目,在同一个文件中有两个着色器,一个片段 着色器和顶点着色器,在片段着色器之前,我有一行代码说#着色器片段和之前 顶点着色器一行表示#着色器顶点,我制作了一个可以解析和分离两个着色器的函数,该函数是 称为parseshader 注意:我有一个struct的原因是我希望它输出两个字符串:一个包含顶点着色器的字符串和一个包含片段着色器的字符串 我希望它输出两个字符串,因为我有另一个函数来编译着色器,该着色器有两个参数:顶点着色器代码和片段着色器代码 s

我在VisualStudio中从事OpenGL项目,在同一个文件中有两个着色器,一个片段 着色器和顶点着色器,在片段着色器之前,我有一行代码说#着色器片段和之前 顶点着色器一行表示#着色器顶点,我制作了一个可以解析和分离两个着色器的函数,该函数是 称为parseshader

注意:我有一个struct的原因是我希望它输出两个字符串:一个包含顶点着色器的字符串和一个包含片段着色器的字符串 我希望它输出两个字符串,因为我有另一个函数来编译着色器,该着色器有两个参数:顶点着色器代码和片段着色器代码

struct shaderProgramSource
{
std::字符串顶点源;
std::字符串碎片源;
};
静态shaderProgramSource parseShader(常量std::string和文件路径){
枚举类着色器类型{
无=-1,顶点=0,片段=1
};  
std::ifstream流(filepath);
std::stringstream ss[2];
shadertype类型=shadertype::无;
std::字符串行;
while(getline(流,行))
{
if(line.find(“#着色器”)!=std::string::npos)
{
if(line.find(“顶点”)!=std::string::npos)
shadertype类型=shadertype::VERTEX;
else if(line.find(“fragment”)!=std::string::npos)
shadertype类型=shadertype::FRAGMENT;
}
其他的
ss[(int)type]。它告诉您代码中可能存在数组索引问题,这是真的

如果在文件的某行中未能成功找到所需的数据,则您试图访问
ss[-1]
,这显然是胡说八道。(根据特定的警告文本,在您的环境中,每个
stringstream
必须有176字节宽。)您不能这样做:
-1
不是有效的数组索引

事实上,即使您以前在文件中找到了一个好的行,您也会犯此错误,因为您一直在声明新的局部变量
type
,而不是分配给现有的变量


如果我正确理解您的代码和目标,我建议进行以下更改:

  • 从那些内部
    if
    语句中删除
    shadertype
    ,将声明更改为赋值
  • if(type!=shadertype::NONE)
    放在
    ss[(int)type]
    访问之前,如果您不希望文件脱离模式,则可能附带一个断言
  • 此外,要使缩进保持一致,以便程序更易于阅读


    struct shaderProgramSource
    {
    std::字符串顶点源;
    std::字符串碎片源;
    };
    静态shaderProgramSource parseShader(常量std::字符串和文件路径)
    {
    枚举类着色器类型
    {
    无=-1,
    顶点=0,
    片段=1
    };  
    std::ifstream流(filepath);
    std::stringstream ss[2];
    shadertype类型=shadertype::无;
    std::字符串行;
    while(getline(流,行))
    {
    if(line.find(“#着色器”)!=std::string::npos)
    {
    if(line.find(“顶点”)!=std::string::npos)
    类型=着色器类型::顶点;
    else if(line.find(“fragment”)!=std::string::npos)
    type=shadertype::FRAGMENT;
    }
    else if(type!=shadertype::NONE)
    {
    ss[(int)type]。它告诉您代码中可能存在数组索引问题,这是真的

    如果在文件的某行中未能成功找到所需的数据,则您试图访问
    ss[-1]
    ,这显然是胡说八道。(根据特定的警告文本,在您的环境中,每个
    stringstream
    必须有176字节宽。)您不能这样做:
    -1
    不是有效的数组索引

    事实上,即使您以前在文件中找到了一个好的行,您也会犯此错误,因为您一直在声明新的局部变量
    type
    ,而不是分配给现有的变量


    如果我正确理解您的代码和目标,我建议进行以下更改:

  • 从那些内部
    if
    语句中删除
    shadertype
    ,将声明更改为赋值
  • if(type!=shadertype::NONE)
    放在
    ss[(int)type]
    访问之前,如果您不希望文件脱离模式,则可能附带一个断言
  • 此外,要使缩进保持一致,以便程序更易于阅读


    struct shaderProgramSource
    {
    std::字符串顶点源;
    std::字符串碎片源;
    };
    静态shaderProgramSource parseShader(常量std::字符串和文件路径)
    {
    枚举类着色器类型
    {
    无=-1,
    顶点=0,
    片段=1
    };  
    std::ifstream流(filepath);
    std::stringstream ss[2];
    shadertype类型=shadertype::无;
    std::字符串行;
    while(getline(流,行))
    {
    if(line.find(“#着色器”)!=std::string::npos)
    {
    if(line.find(“顶点”)!=std::string::npos)
    类型=着色器类型::顶点;
    else if(line.find(“fragment”)!=std::string::npos)
    type=shadertype::FRAGMENT;
    }
    else if(type!=shadertype::NONE)
    {
    
    ss[(int)type]如果类型不是shadertype::NONE,则断言

    也许这会给你一些提示


    从我在代码中看到的情况来看,有一个执行路径,其中类型可能仍然是shadertype::NONE。在这种情况下,您取消了对坏索引的引用。

    如果类型不是shadertype::NONE,则断言

    也许这会给你一些提示

    从我在代码中看到的情况来看,有一个执行路径,其中的类型可能仍然是shadertype::NONE
            struct shaderProgramSource
        {
            std::string VertexSource;
            std::string FragmentSource;
        };
    
        static shaderProgramSource parseShader(const std::string& filepath) {  
            enum class shadertype{
            NONE = -1 ,VERTEX = 0 ,FRAGMENT = 1
        };  
    std::ifstream stream(filepath);
    std::stringstream ss[2];
    shadertype type = shadertype::NONE;
    std::string line;
    while (getline(stream, line))
    {
        if (line.find("#shader") != std::string::npos)
        {
            if (line.find("vertex") != std::string::npos)
                shadertype type = shadertype::VERTEX;
            else if(line.find("fragment") != std::string::npos)
                shadertype type = shadertype::FRAGMENT;
    
        }
     else
            ss[(int)type] << line << "\n";
    }
    
    return { ss[0].str(),ss[1].str() };
        }
    
    struct shaderProgramSource
    {
        std::string VertexSource;
        std::string FragmentSource;
    };
    
    static shaderProgramSource parseShader(const std::string& filepath)
    {
        enum class shadertype
        {
            NONE     = -1,
            VERTEX   = 0,
            FRAGMENT = 1
        };  
    
        std::ifstream stream(filepath);
        std::stringstream ss[2];
        shadertype type = shadertype::NONE;
    
        std::string line;
        while (getline(stream, line))
        {
            if (line.find("#shader") != std::string::npos)
            {
                if (line.find("vertex") != std::string::npos)
                    type = shadertype::VERTEX;
                else if(line.find("fragment") != std::string::npos)
                    type = shadertype::FRAGMENT;
            }
            else if (type != shadertype::NONE)
            {
                ss[(int)type] << line << "\n";
            }
            else
            {
                // Got non-introductory line out of sequence! Don't know
                // what type to use! Consider asserting, or throwing an
                // exception, or something, depending on how defensive you
                // want to be with respect to the input file format.
            }
        }
    
        return { ss[0].str(), ss[1].str() };
    }
    
    if (line.find("#shader") != std::string::npos)
    {
        if (line.find("vertex") != std::string::npos)
            shadertype type = shadertype::VERTEX;
        else if(line.find("fragment") != std::string::npos)
            shadertype type = shadertype::FRAGMENT;
    
    }
    
    if (line.find("#shader") != std::string::npos)
    {
        if (line.find("vertex") != std::string::npos)
            type = shadertype::VERTEX;
        else if(line.find("fragment") != std::string::npos)
            type = shadertype::FRAGMENT;
    
    }