繁体   English   中英

访问字符串数组中的元素0会使其崩溃

[英]accessing element 0 in string array makes it crash

我正在声明以下数组

  string names[4]  = { "grass" , "smallGrassBlock" , "dirt" , "sand" };

接着

extern string names [];`

但是当我这样做时它会崩溃:

string ss=names[0];

所有相关代码:

主要:

     #include<stdlib.h>
        #include<windows.h>
        #include<GL/glut.h>
        #include<GL/freeglut.h>
        #include<iostream>
        #include <vector>
        #include "include/Block.h"
        #include <string>

        extern string names [];
        void initTextures();
        GLint GetTexture(string file);
        void IdleFunction();
        void removeBlock(int x,int y);
        int xOffset,yOffset = 0;
        using namespace std;
        string to_string(int number);
        void placeBlock(int x,int y);
        void drawBitmapText(char *string, float x, float y, float z);
        void reshape(int w, int h);
        void render(void);
        void keyboard(unsigned char c,int x,int y);
        void mouse(int button,int state, int x, int y);
        void arrows(int key, int x, int y );

        std::vector <Block> blockList;
        GLuint  textures[1];
        int screenXSize=800;
        int screenYSize=800;
        int selectedBlockType = 1;
        int main(int argc, char ** argv){


        blockList.push_back(Block(0,-111,-111));


        string s = blockList.at(0).getName();



        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
        glutInitWindowPosition(100,100);
        glutInitWindowSize(screenXSize,screenYSize);
        glutCreateWindow("TITLE");
        glutDisplayFunc(render);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyboard);
        glutIdleFunc(IdleFunction);
        glutSpecialFunc(arrows);
        glutMouseFunc(mouse);

        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        initTextures();

        glutMainLoop();








        }
        void initTextures(){



         for(int i =0; i<=2; i++){
            string file = "C:/Users/Samuel/Documents/Documents/projects/test/MapCreator/"+names[i]+".png";


            textures[i]=GetTexture(file);










}


        }
       GLint GetTexture(std::string Filename)
{
    GLuint tex_ID;

    tex_ID = SOIL_load_OGL_texture(
                Filename.c_str(),
                SOIL_LOAD_AUTO,
                SOIL_CREATE_NEW_ID,
                SOIL_FLAG_POWER_OF_TWO
                | SOIL_FLAG_MIPMAPS
                | SOIL_FLAG_MULTIPLY_ALPHA
                | SOIL_FLAG_COMPRESS_TO_DXT
                | SOIL_FLAG_DDS_LOAD_DIRECT
                | SOIL_FLAG_INVERT_Y
                );

        if( tex_ID > 0 )
        {

            glEnable( GL_TEXTURE_2D );
            glBindTexture( GL_TEXTURE_2D, tex_ID );

            return tex_ID;
        }
        else{
            cout<<"Texture failed to load";
            return 0;
}}

        void arrows(int key, int x, int y ){

        if(key==101){


           selectedBlockType++;

           if(selectedBlockType > blockList.at(0).getTotal()){
            selectedBlockType=1;

           }


        }
        if(key==103){
            selectedBlockType--;
           if(selectedBlockType < 0){
            selectedBlockType=blockList.at(0).getTotal();

           }
        }

        }
        void IdleFunction()
{
    glutPostRedisplay();
}

        void mouse(int button,int state, int x, int y){
y+=15;
x-=15;

            if(button==GLUT_RIGHT_BUTTON){
        removeBlock(x,y);

            }
             if(button==GLUT_LEFT_BUTTON){
        placeBlock(x,y);

            }


        }
        void keyboard(unsigned char c,int x,int y){

    //wasd
        if(c==119){
           yOffset+=30;
        }
        if(c==97){
            xOffset-=30;
        }
            if(c==115){
            yOffset-=30;

        }
        if(c==100){
            xOffset+=30;
        }
            if (c==27){
                exit(0);
            }


        }

    void removeBlock(int x, int y){

    y-= yOffset;
    x+=xOffset;
    y-=400;
    y*=-1;
    y+=400;


int xmod = x%30;
    x-=xmod;
    if(xmod>=15){
        x+=30;
    }
    int ymod = y%30;
    y-=ymod;
    if(ymod>=15){
        y+=30;
    }
   for(int i = 0; i<blockList.size();i++){

        if(blockList.at(i).getX()==x){

           if(blockList.at(i).getY()==y){


            blockList.erase(blockList.begin()+i);


           }
        }
    }



    }


        void placeBlock(int x, int y){
            y-= yOffset;
            x+=xOffset;
    y-=400;
    y*=-1;
    y+=400;

    //gets it on the grid lines
    int xmod = x%30;
    x-=xmod;
    if(xmod>=15){
        x+=30;
    }
    int ymod = y%30;
    y-=ymod;
    if(ymod>=15){
        y+=30;
    }

    bool there=false;

    for(int i = 0; i<blockList.size();i++){
        if(blockList.at(i).getXMin()<=x&&blockList.at(i).getXMax()>=x&&blockList.at(i).getYMin()<=y&&blockList.at(i).getYMax()>=y){
            there=true;
            cout<<"cannot place there!"<<endl;
        }
    }
    if(!there){



    blockList.push_back(Block(selectedBlockType, x, y));



    }

        }

    void reshape(int w, int h)
    {

       glViewport (0, 0, (GLsizei) w, (GLsizei) h);
       glMatrixMode (GL_PROJECTION);
       glLoadIdentity ();
       gluOrtho2D (0, (GLdouble) 800, 0, (GLdouble) 800);
       glutReshapeWindow(screenXSize,screenYSize);

    }
    void drawBitmapText(char *string, float x, float y, float z)
    {
        char *c;
        glRasterPos3f(x, y, z);

        for (c=string; *c != '\0'; c++)
        {
            glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
        }
    }
        void render(void){


        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


    for(int i = 0; i<blockList.size();i++){


     blockList.at(i).render(xOffset,yOffset,textures[blockList.at(i).getType()]);

    }

    glColor3f(0,0,0);
    //This creates the grid
    //Should be broken every 30
        for(float f = 0;f<800; f+=30){
        glBegin(GL_LINE_STRIP);
            glVertex2f(f,0);
            glVertex2f(f,800);
            glEnd();
            glBegin(GL_LINE_STRIP);
            glVertex2f(0,f);
            glVertex2f(800,f);
            glEnd();

        }

    glColor3f(0.35,0.35,0.35);
    string s = to_string(selectedBlockType);

string ss=names[0];
cout<<"YAY"<<endl;
cout<<ss;
cout<<endl<<"Selected"<<selectedBlockType;
    s=s+" "+names[selectedBlockType];
cout<<"YAY"<<endl;


    char *c = (char*)s.c_str();
        drawBitmapText(c,5,760,0);




        glutSwapBuffers();

        glFlush();

        }

        string to_string(int number){
        string number_string = "";

        char ones_char;
        int ones = 0;
        while(true){
            ones = number % 10;
            switch(ones){
                case 0: ones_char = '0'; break;
                case 1: ones_char = '1'; break;
                case 2: ones_char = '2'; break;
                case 3: ones_char = '3'; break;
                case 4: ones_char = '4'; break;
                case 5: ones_char = '5'; break;
                case 6: ones_char = '6'; break;
                case 7: ones_char = '7'; break;
                case 8: ones_char = '8'; break;
                case 9: ones_char = '9'; break;

            }
            number -= ones;
            number_string = ones_char + number_string;
            if(number == 0){
                break;
            }
            number = number/10;
        }
        return number_string;
    }

块头:

    #ifndef BLOCK_H
#define BLOCK_H
#include <string>
#include <iostream>
#include<GL/freeglut.h>
#include <soil.h>


using namespace std;

class Block
{
    public:
        int getXMax();
        int getXMin();
        int getYMin();
        int getYMax();
        Block(int,int,int);
       int getX();
       int getY();
       static int getTotal();
       int getType();
       void render(int,int,GLuint);
       string getName();
       string getNType(int i);
        virtual ~Block();
    protected:
    private:

 int x,xsize,ysize,y,Type,size;

};

#endif // BLOCK_H

块:

     #include "../include/Block.h"
        #include <string>
        #include <iostream>
        #include<GL/freeglut.h>
        #include <soil.h>

        using namespace std;



  string names[4]  = { "grass" , "smallGrassBlock" , "dirt" , "sand" };




        Block::Block(int type, int X,int Y)
        {
            size=3;
            Type=type;
            x=X;
            y=Y;

            switch(Type){
    case 0:
        xsize=546;
        ysize=217;
        break;
        case 1:

        xsize = 200;
        ysize=95;
        break;
        default:
            xsize=30;
            ysize=30;
            break;
            }

        }
         int Block:: getTotal(){
        return 3;
        }

        int Block::getX(){
            return x;
        }
        int Block::getY(){
            return y;
        }
        int Block::getXMin(){
        return x-(xsize/2);
        }
        int Block::getYMin(){
        return y-(ysize/2);
        }
        int Block::getXMax(){
        return x+(xsize/2);
        }
        int Block::getYMax(){
        return y+(ysize/2);
        }
        int Block::getType(){
        return Type;
        }
        string Block::getName(){




        return names[Type];;
        }
        string Block::getNType(int i){
        return names[i];

        }

        void Block:: render(int xOffset,int yOffset,GLuint texture){

        if(texture==0){


        }



        x-=xOffset;
        y-=yOffset;
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         glEnable( GL_BLEND );
         glClearColor(0.0,0.0,0.0,0.0);

         glEnable(GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, texture);
            glBegin(GL_POLYGON);
                glTexCoord2f(1,1);  glVertex2f(x+(xsize/2), y+(ysize/2));
              glTexCoord2f(1,0);  glVertex2f(x+(xsize/2), y-(ysize/2));

                glTexCoord2f(0,0);  glVertex2f(x-(xsize/2), y-(ysize/2));
              glTexCoord2f(0,1);  glVertex2f(x-(xsize/2), y+(ysize/2));




            glEnd();
            glDisable(GL_TEXTURE_2D);
            glDisable( GL_BLEND );
            glClearColor(1.0,1.0,1.0,0.0);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


             glPopAttrib();
        x+=xOffset;
        y+=yOffset;

        }
        Block::~Block()
        {


            //dtor
        }

更多信息:获取0以外的任何有效元素时,程序不会崩溃,也不会提供错误消息。

目前尚不清楚您是如何实施您的计划的。 以下是我写的测试用例。 使用Microsoft C ++编译器(cl.exe)运行正常没有任何问题。

TEST.CPP

#include <iostream>
#include <string>
using namespace std;
extern string names[];

int main()
{
  cout << names[0] << endl; 
  return 0;
}

测试2.cpp

#include <string>
using namespace std;
string names[4]  = { "grass" , "smallGrassBlock" , "dirt" , "sand" };

编译命令:

cl.exe test.cpp  test2.cpp

如果您的代码是这样的,那么这很好

main.cpp中

extern string names[];
int main() {
  string ss = names[3];
  cout <<  ss << endl; 
  cin.get();
}

string.cpp

#include <string>
using namespace std;
string names[4]  = { "grass" , "smallGrassBlock" , "dirt" , "sand" };

注意:将这两个文件保存在同一个项目中。

或者将.cpp文件一起编译

gcc main.cpp other.cpp

暂无
暂无

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

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