简体   繁体   English

出现未解决的外部错误

[英]Getting Unresolved External error

I have made a class and it compiles with no syntax errors, but I get 6 unresolved external symbols? 我做了一个类,并且编译时没有语法错误,但是我得到了6个未解析的外部符号?

THE CLASS: 班级:

struct CELL {
private:
    static bool haslife;
static int x;
static int y;
public:

    static bool has_life()
    {
        return haslife;
    }

    static void set_coords(int xcoord, int ycoord)
    {
        x = xcoord;
        y = ycoord;
    }
    static void get_coords(int &xcoord, int &ycoord)
    {
        xcoord = x;
        ycoord = y;
    }


};


class cell_grid {
private:
static int cell_size;
static int cell_count_x;
static int cell_count_y;
CELL **cell;
public:
    cell_grid();
    cell_grid(unsigned int width,unsigned int height, unsigned int cellsize)
    {

        //set size based on cellsize

        this->cell_size = cellsize;
        this->cell_count_x = floor((double)width / this->cell_size);
        this->cell_count_y = floor((double)height / this->cell_size);


        this->cell = new CELL*[this->cell_count_y];

        for(int i = 0; i < this->cell_count_y; i++)
        {
            cell[i] = new CELL[this->cell_count_x];
        }

        for(int y = 0; y < this->cell_count_y; ++y)
        {
            for(int x = 0; x < this->cell_count_x; ++x)
            {
                int cur_x = x * this->cell_size;
                int cur_y = y * this->cell_size;
                this->cell[x][y].set_coords(cur_x,cur_y);
            }
        }

    } //end of constructor

    static int get_cell_size()
    {
        return cell_size;
    }
static void render(BITMAP *buff)
{
    circlefill(buff,70,70,60,makecol(27,37,0));

}


};

MAIN 主要

int main()
{
    start_allegro();
    cell_grid *grid = new cell_grid(scr_w,scr_h,10);
    grid->render(buffer);


        //Main Loop
    while (!done && !key[KEY_ESC]) //until 'X' pressed or ESC
{
//***** Start Main Code Here *****
    while (speed_counter > 0)
    {



                 //render the buffer to the screen

            blit(
            buffer,
            screen,
            0,0,0,0,
            scr_w,
            scr_h);

            clear_bitmap(buffer);

        speed_counter --;
    }
//***** End Main Code Here *****
rest(1); //Normalize cpu usage
}
    return 0;
}
END_OF_MAIN()

Thanks 谢谢

Don't define all of the class variables as static. 不要将所有类变量都定义为静态。
When you define a data member as static it means there is only one single instance of it. 当您将数据成员定义为静态时,这意味着它只有一个实例。 This doesn't seem to be what you want to do here. 这似乎不是您要在此处执行的操作。
Instead of 代替

private:
    static bool haslife;
    static int x;
    static int y;

write: 写:

private:
    bool haslife;
    int x;
    int y;

Further more, when you define a static member, you need to define it again in the CPP file and initialize it with a value. 此外,定义静态成员时,需要在CPP文件中再次定义它,并使用值对其进行初始化。 It doesn't look like you're doing that and that's why you're getting the linker errors. 看来您没有这样做,所以才出现链接器错误。

Also, next time you post something, make sure you actually ask a question rather than just simply stating facts. 另外,下次您发布内容时,请确保您确实提出了一个问题,而不仅仅是陈述事实。

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

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