簡體   English   中英

Visual C ++和Allegro5:無法從頭文件的抽象類中識別對象

[英]Visual C++ and Allegro5: cannot recognize objects from abstract class from header file

我是Allegro的初學者,並且對C ++的適應不是很好,但是我了解這種語言,所以我希望編寫象棋游戲。 但是由於我想在該游戲中使用圖像,因此我研究了Allegro 5對其進行編程。 我想出了一個(當然不是完整的)頭文件來初始化游戲中的每個棋子:

片數

#pragma once
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <iostream>
#include <string>

using namespace std;

class Piece
{
    private:

        int pieceX;
        int pieceY;
        bool inGame;
        char pieceColor;
        string imgAddress;
        ALLEGRO_BITMAP *pieceImage;

    public:

        Piece(int pieceX, int pieceY, bool inGame, char pieceColor, string imgAddress)
        {
            this->pieceX = pieceX;
            this->pieceY = pieceY;
            this->inGame = inGame;
            this->pieceColor = pieceColor;
            this->imgAddress = imgAddress;
            pieceImage = al_load_bitmap(imgAddress.c_str());
        }

        int getPieceX()
        {
            return pieceX;
        }

        int getPieceY()
        {
            return pieceY;
        }

        bool isInGame()
        {
            return inGame;
        }

        char getPieceColor()
        {
            return pieceColor;
        }

        string getImageAddress()
        {
            return imgAddress;
        }

        void setPieceX(int newx)
        {
            pieceX = newx;
        }

        void setPieceY(int newy)
        {
            pieceY = newy;
        }

        void setinGame(bool newingame)
        {
            inGame = newingame;
        }

        void setImageAddress(string newimgaddr)
        {
            imgAddress = newimgaddr;
        }

        void reloadImage()
        {
            pieceImage = al_load_bitmap(imgAddress.c_str());
        }

        Piece(void)
        {

        }

        ~Piece(void)
        {

        }

};

其他的一切都將在主類Chess.cpp中處理

#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include "Piece.h"
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

const int SIDELENGTH = 50;
const int ROWCOLSPAN = 8;
const int WINDOW_WIDTH = (SIDELENGTH * ROWCOLSPAN) + 240;
const int WINDOW_HEIGHT = (SIDELENGTH * ROWCOLSPAN) + 40; 
const float FPS = 60;
const int MAXPIECES = ROWCOLSPAN * 2;
const int MAXSPACES = ROWCOLSPAN * ROWCOLSPAN;

setupBoard(Piece [MAXPIECES], Piece [MAXPIECES], char [MAXSPACES]);

int main()
{
    ALLEGRO_DISPLAY *gameDisplay;

    if (!al_init()) cout << "Failed to load Allegro 5 for Chess++..." << endl;

    gameDisplay = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
    al_set_window_title(gameDisplay, "Chess++");

    if (!gameDisplay) cout << "Couldn't create Allegro 5 display for Chess++..." << endl;

    al_init_primitives_addon(); //allows to load primitives
    al_install_keyboard(); //allows for keyboard use*/
    al_install_mouse(); //installs the mouse to be able to use it
    al_init_image_addon(); //prepares image loading

    ALLEGRO_EVENT_QUEUE *gameQueue = al_create_event_queue(); //queue that receives events and acts them in order
    al_register_event_source(gameQueue, al_get_keyboard_event_source()); //registers keyboard events to be recognized within event_queue

    ALLEGRO_KEYBOARD_STATE keyState;
    ALLEGRO_TIMER *gameTimer = al_create_timer(1.0 / FPS);

    al_register_event_source(gameQueue, al_get_keyboard_event_source()); //registers keyboard events to be recognized within event_queue
    al_register_event_source(gameQueue, al_get_timer_event_source(gameTimer)); //registers a timer to work within event_queue
    al_register_event_source(gameQueue, al_get_display_event_source(gameDisplay)); //registers the window to be able to give it events
    al_register_event_source(gameQueue, al_get_mouse_event_source()); //register the installed mouse

    bool gameOver = false;
    bool draw = false;
    bool quitclose = false;

    int X = 0;
    int Y = 0;

    Piece blackPieces[MAXPIECES];
    Piece whitePieces[MAXPIECES];

    //The 8 by 8 board with the pieces; lowercase for black, uppercase for white
    char chessboard[] = "rnbqkbnr\n"
                        "pppppppp\n" 
                        "--------\n"
                        "--------\n"
                        "--------\n"
                        "--------\n"
                        "--------\n"
                        "PPPPPPPP\n"
                        "RNBQKBNR";

    //setupBoard(blackPieces, whitePieces, chessboard);

    al_start_timer(gameTimer); //starts the specified timer

    gameloop:
    while(!gameOver)
    {
        ALLEGRO_EVENT gameEvents; //create receiver for events
        al_wait_for_event(gameQueue, &gameEvents); //waits for an event from event_queue to be passed on to ALLEGRO_EVENT events

        if (gameEvents.type == ALLEGRO_EVENT_KEY_UP/*DOWN*/)
        {

        }
        else if (gameEvents.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            quitclose = true;
            gameOver = true;
        }
        else if (gameEvents.type == ALLEGRO_EVENT_MOUSE_AXES) //determines the movement of the mouse coordinates
        {

        }
        else if (gameEvents.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) //if a mouse button is pressed
        {

        }

        if (gameEvents.type == ALLEGRO_EVENT_TIMER)
        {
            draw = true;
        }

        if (draw)
        {
            draw = false;

            int squareX = 20;
            int squareY = 20;
            bool colorchange = true;
            for (int i = 1; i <= MAXSPACES;i++)
            {               
                ALLEGRO_COLOR squarecolor = al_map_rgb(255, 255, 255);
                if (!colorchange) squarecolor = al_map_rgb(0, 100, 0);

                al_draw_filled_rectangle(squareX, squareY, squareX + SIDELENGTH, squareY + SIDELENGTH, squarecolor);
                squareX += SIDELENGTH;
                if (i % ROWCOLSPAN != 0) colorchange = !colorchange;

                if (i % ROWCOLSPAN == 0)
                {
                    squareY += SIDELENGTH;
                    squareX = 20;
                }

            }

            al_flip_display();          
            al_clear_to_color(al_map_rgb(0, 0, 0)); //clears the canvas (display) like Java's repaint()
        }

    }

    if (quitclose)
    {
        int quit = al_show_native_message_box(gameDisplay, "CHES++: QUIT", "ARE YOU SURE YOU WANNA QUIT?", "Please select YES or NO", NULL, ALLEGRO_MESSAGEBOX_YES_NO);
        if (quit == 0)
        {
            gameOver = false;
            goto gameloop;
        }
    }

    al_destroy_display(gameDisplay);

    /*cin.sync();
    cout << "\n\nPress any key to finish...";
    cin.get();*/
    return 0;
}

void setupBoard(Piece bp[MAXPIECES], Piece wp[MAXPIECES], char cb[])
{
    int barrindex = 0;
    int warrindex = 0;
    int x = 20;
    int y = 20;

    for (int i = 0; i < MAXSPACES; i++)
    {
        if (cb[i] == 'r')
        {
            bp[barrindex] = new Piece(x, y, true, 'b', "images/br.png");
            barrindex++;
            x += SIDELENGTH;
        }     
        if (cb[i] == 'n')
        {
            bp[barrindex] = new Piece(x, y, true, 'b', "images/bn.png");
            barrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'b')
        {
            bp[barrindex] = new Piece(x, y, true, 'b', "images/bb.png");
            barrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'q')
        {
            bp[barrindex] = new Piece(x, y, true, 'b', "images/bq.png");
            barrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'k')
        {
            bp[barrindex] = new Piece(x, y, true, 'b', "images/bk.png");
            barrindex++;
            x += SIDELENGTH;
        } 
        if (cb[i] == 'p')
        {
            bp[barrindex] = new Piece(x, y, true, 'b', "images/bp.png");
            barrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'R')
        {
            wp[warrindex] = new Piece(x, y, true, 'w', "images/wr.png");
            warrindex++;
            x += SIDELENGTH;
        }     
        if (cb[i] == 'N')
        {
            wp[warrindex] = new Piece(x, y, true, 'w', "images/wn.png");
            warrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'B')
        {
            wp[warrindex] = new Piece(x, y, true, 'w', "images/wb.png");
            warrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'Q')
        {
            wp[warrindex] = new Piece(x, y, true, 'w', "images/wq.png");
            warrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == 'K')
        {
            wp[warrindex] = new Piece(x, y, true, 'w', "images/wk.png");
            warrindex++;
            x += SIDELENGTH;
        } 
        if (cb[i] == 'P')
        {
            wp[warrindex] = new Piece(x, y, true, 'w', "images/wp.png");
            warrindex++;
            x += SIDELENGTH;
        }
        if (cb[i] == '-')
        {
            x += SIDELENGTH;
        } 
        if (cb[i] == '\n')
        {
            y += SIDELENGTH;
            x = 20;
        }
    }
}

在添加setupBoard()函數和兩個Piece對象(blackPieces和whitePieces)之前,我的程序將運行,並且將顯示一個不錯的“棋盤”,該棋盤基本上是使用Allegro的原始庫繪制的64個正方形,所以我認為我已經正確安裝了庫和驅動程序。 但是我遇到的錯誤是我無法企及的:

  1. 首先,setupBoard是一個空白,僅用於修改其他變量以組織代碼。 但是Intellisense給我一個錯誤: this declaration has no storage class or type specifier

  2. 我沒有收到#include "Piece.h"錯誤,但是在數組blackPieces和whitePieces的聲明中,我得到的identifier "Piece" is undefined

  3. 在Chess.cpp底部的setupBoard函數中,我通讀了來自棋c的char數組cb,該數組具有特定的字符以將棋子放置在棋盤中。 並根據哪個字符,將Piece對象動態分配給其各自的Piece數組,如下所示:

bp[barrindex] = new Piece(x, y, true, 'b', "images/br.png");

然后,控制板的x和y的整數會相應增加,以將它們繪制在不同的位置。 但是,雖然除了new Piece(似乎沒有錯誤,我猜這是由於錯誤2引起的,但出現此錯誤:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Piece *' (or there is no acceptable conversion)

我確定我一定分配錯了,但是我不知道怎么做,我從來沒有遇到過這樣的錯誤。 現在,我總是可以嘗試用Java或C#進行國際象棋游戲,但我想用C ++進行某種游戲(我認為使AI與邏輯知識無關的邏輯)。 請,如果有人知道如何解決這些錯誤,我將非常感謝!

setupBoard(Piece [MAXPIECES], Piece [MAXPIECES], char [MAXSPACES]);

您需要在此之前留一個void

這些是不同的東西:

Piece bp[MAXPIECES]
Piece* bp[MAXPIECES]

擁有方式,您需要執行以下操作:

wp[warrindex] = Piece(x, y, true, 'w', "images/wn.png");

要了解兩者之間的區別,請閱讀堆棧與堆內存分配。 您可能真的想通過Piece* bp[MAXPIECES]使用堆並保留new調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM