首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++设置类值

C++设置类值
EN

Stack Overflow用户
提问于 2015-04-10 15:16:25
回答 3查看 102关注 0票数 1

最近,我开始用C++编写我需要为下一所学校编写的录取作业,现在我看到了一个大问题,我不知道如何解决它。当我试图运行我的代码时,它会说"Project.exe已经停止工作了“。我试图在互联网上找到解决方案,但我没有找到任何解决问题的方法。这是我的代码( SDL部分不是引起麻烦的部分,但为了代码的完整性,我还是将它们包括在内):

代码语言:javascript
复制
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

class particle {
    public:
        void setxPos(int xx){
            xP=xx;
        };

        void setyPos(int yy){
            yP=yy;
        };

        void setxVel(int xv){
            xVel=xv;
        };

        void setyVel(int yv){
            yVel=yv;
        };

        int getxPos(){
            return (xP);
        };

        int getyPos(){
            return (yP);
        };

    protected:
        int xP, yP;
        int xVel = 0;
        int yVel = 0;


};

void draw(SDL_Renderer* tempRend, particle drawJelly[6][4]){    //Function created for drawing the lines between the particles


    SDL_RenderDrawLine( tempRend, drawJelly[1][1].getxPos(), drawJelly[1][1].getyPos(), drawJelly[2][1].getxPos(), drawJelly[2][1].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[2][1].getxPos(), drawJelly[2][1].getyPos(), drawJelly[3][1].getxPos(), drawJelly[3][1].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[3][1].getxPos(), drawJelly[3][1].getyPos(), drawJelly[4][1].getxPos(), drawJelly[4][1].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[4][1].getxPos(), drawJelly[4][1].getyPos(), drawJelly[5][1].getxPos(), drawJelly[5][1].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[5][1].getxPos(), drawJelly[5][1].getyPos(), drawJelly[6][1].getxPos(), drawJelly[6][1].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[6][1].getxPos(), drawJelly[6][1].getyPos(), drawJelly[6][2].getxPos(), drawJelly[6][2].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[6][2].getxPos(), drawJelly[6][2].getyPos(), drawJelly[6][3].getxPos(), drawJelly[6][3].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[6][3].getxPos(), drawJelly[6][3].getyPos(), drawJelly[6][4].getxPos(), drawJelly[6][4].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[6][4].getxPos(), drawJelly[6][4].getyPos(), drawJelly[5][4].getxPos(), drawJelly[5][4].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[5][4].getxPos(), drawJelly[5][4].getyPos(), drawJelly[4][4].getxPos(), drawJelly[4][4].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[4][4].getxPos(), drawJelly[4][4].getyPos(), drawJelly[3][4].getxPos(), drawJelly[3][4].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[3][4].getxPos(), drawJelly[3][4].getyPos(), drawJelly[2][4].getxPos(), drawJelly[2][4].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[2][4].getxPos(), drawJelly[2][4].getyPos(), drawJelly[1][4].getxPos(), drawJelly[1][4].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[1][4].getxPos(), drawJelly[1][4].getyPos(), drawJelly[1][3].getxPos(), drawJelly[1][3].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[1][3].getxPos(), drawJelly[1][3].getyPos(), drawJelly[1][2].getxPos(), drawJelly[1][2].getyPos() );
    SDL_RenderDrawLine( tempRend, drawJelly[1][2].getxPos(), drawJelly[1][2].getyPos(), drawJelly[1][1].getxPos(), drawJelly[1][1].getyPos() );

    SDL_RenderPresent( tempRend );                                                 
    SDL_Delay(100);     //Wait .5 seconds for clarity of the drawing
    }

void position(particle posJelly[6][4]){

    int a=0;

    while (a<6){        //Give all the jelly particles a position in the 20x20 grid
        int b=0;
        while(b<4){
            posJelly[a][b].setxPos((20*a));
            posJelly[a][b].setyPos((20*b));
        b--;
        }
    a--;
    }
}

int main( int argc, char* args[] )
{
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;
    SDL_Surface* surface = NULL;

    SDL_Init( SDL_INIT_VIDEO );

    window = SDL_CreateWindow( "Jelly Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    renderer = SDL_CreateRenderer( window, -1, 0);
    surface = SDL_GetWindowSurface( window );

    SDL_SetRenderDrawColor( renderer, 0, 255, 0, 255 );
    SDL_RenderPresent( renderer );

    particle jelly[6][4];           //New object from class "particle" with name jelly

    position(jelly);                //Run function "position()" on particle jelly to set starting coördinates

    bool inGame=true;              //Start the game-loop

    while(inGame){
        draw(renderer, jelly);
        inGame=false;
    }

    SDL_Delay( 2000 );              //Wait 2 seconds

    SDL_DestroyWindow( window );    //Destroy the window "window"

    SDL_Quit();                     //Quit SDL

    return 0;                       //End the program
}

现在,我已经找到了原因所在,这部分就是:

代码语言:javascript
复制
void position(particle posJelly[6][4]){

    int a=0;

    while (a<6){        //Give all the jelly particles a position in the 20x20 grid
        int b=0;
            while(b<4){
            posJelly[a][b].setxPos((20*a));
            posJelly[a][b].setyPos((20*b));
            b--;
        }
        a--;
    }
}

因为当我删除

代码语言:javascript
复制
position(jelly);

或移除

代码语言:javascript
复制
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));

在位置函数中,它不会产生错误。(它不能正常工作,因为它没有正确的坐标可供绘制,但它“工作”)

再次,我已经搜索了许多其他文章和教程,为什么这不工作,或如何使这可以工作,但还没有发现任何东西。

“游戏”将是一团果冻,由24个粒子(6x4,但只有外部粒子将被画在屏幕上)组成,它们与邻居有着理想的距离,然后当它们彼此移动得更近或更远时,它们会被拉回/推回那个距离,从而产生一团果冻的“弹跳”效应。

请帮帮我!

而且,我知道just ()函数不是很好的工具,但无论如何它不会是一个完全发布的游戏,而且由于blob总是6乘4,这是一种比通过循环和其他方法寻找外部粒子更容易的绘制方法。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-10 15:22:15

在这种情况下,错误与数组索引的负值有关。你会看到这样的错误,当你的程序引发了盗用,而你没有处理它。

你应该改变:

a--b--用于a++b++

当您的代码第一次到达行时:

代码语言:javascript
复制
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));

a==0b==0,但是第二次(在a--b--之后),您会有这样的东西:

代码语言:javascript
复制
posJelly[-1][-1].setxPos((20*a));  // illegal C++ no negative inidices are allowed
posJelly[-1][-1].setyPos((20*b));  // illegal C++ no negative inidices are allowed
票数 0
EN

Stack Overflow用户

发布于 2015-04-10 15:20:04

您正在尝试使用负索引访问数组。在第一次迭代中,将b设置为0,然后将其缩减为-1。然后在下一次迭代中使用b,程序就会繁荣起来。您可能打算使用++而不是--。

票数 1
EN

Stack Overflow用户

发布于 2015-04-10 15:19:52

将递减操作符替换为增量:a--b--,分别用a++b++替换。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29565017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档