未定义的引用

发布于 2024-09-26 18:46:47 字数 2612 浏览 0 评论 0原文

链接器错误:

$ make
g++ -Wall -g main.cpp SDL_Helpers.cpp Game.cpp DrawableObject.cpp `sdl-config --cflags --libs` -lSDL_mixer
/tmp/ccdxzrej.o: In function `Game':
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
collect2: ld returned 1 exit status
make: *** [all] Error 1

main.cpp:

#include "Game.h"

int main()
{
    Game g;
    return 0;
}

Game.h:

#ifndef BRETT_GAME_H
#define BRETT_GAME_H

#include <cmath>
#include "SDL.h"
#include "SDL_Helpers.h"
#include <vector>

class DrawableObject;

class Game
{
public:
    SDL_Surface * screen;
    std::vector<DrawableObject*> sprites;

    Game();
};

#endif

Game.cpp:

#include "Game.h"
#include "DrawableObject.h"
#include "Player.h"

Game::Game()
{   
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
        return;

    atexit(SDL_Quit);
    screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF);

    if (screen == NULL)
        return;

    sprites.push_back(new Player(this));

    while (true)
    {
        SDL_Event * event = NULL;
        while(SDL_PollEvent(event) && event)
        {
            if(event->type == SDL_QUIT)
                return;
        }

        SDL_LockSurface(screen);

        for (uint32 i=0; i<sprites.size(); ++i)
        {
            sprites[i]->update(event);
        }

        SDL_FreeSurface(screen);
        SDL_Flip(screen);
    }
}

DrawableObject.h:

#ifndef DRAWABLE_OBJECT_H
#define DRAWABLE_OBJECT_H

#include "SDL.h"
#include "SDL_Helpers.h"

class Game;

class DrawableObject
{
public:
    Game * const game;

    DrawableObject(Game * const game_);

    virtual void update(SDL_Event *event) = 0;

};

#endif

DrawableObject.cpp:

#include "DrawableObject.h"
#include "Game.h"

DrawableObject::DrawableObject(Game * const game_)
    :   game(game_)
{

}

Player.h:

#ifndef PLAYER_H
#define PLAYER_H

#include <cmath>
#include "DrawableObject.h"

class Player : public DrawableObject
{
public:
    Player(Game * const game);

    void update(SDL_Event *event);
};

#endif

Player.cpp:

#include "Player.h"
#include "Game.h"

Player::Player(Game * const game)
    : DrawableObject(game)
{

}

void Player::update(SDL_Event *event)
{
    draw_circle(game->screen, 100, 100, 50, 0xff000000);
}

Linker Error:

$ make
g++ -Wall -g main.cpp SDL_Helpers.cpp Game.cpp DrawableObject.cpp `sdl-config --cflags --libs` -lSDL_mixer
/tmp/ccdxzrej.o: In function `Game':
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
collect2: ld returned 1 exit status
make: *** [all] Error 1

main.cpp:

#include "Game.h"

int main()
{
    Game g;
    return 0;
}

Game.h:

#ifndef BRETT_GAME_H
#define BRETT_GAME_H

#include <cmath>
#include "SDL.h"
#include "SDL_Helpers.h"
#include <vector>

class DrawableObject;

class Game
{
public:
    SDL_Surface * screen;
    std::vector<DrawableObject*> sprites;

    Game();
};

#endif

Game.cpp:

#include "Game.h"
#include "DrawableObject.h"
#include "Player.h"

Game::Game()
{   
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
        return;

    atexit(SDL_Quit);
    screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF);

    if (screen == NULL)
        return;

    sprites.push_back(new Player(this));

    while (true)
    {
        SDL_Event * event = NULL;
        while(SDL_PollEvent(event) && event)
        {
            if(event->type == SDL_QUIT)
                return;
        }

        SDL_LockSurface(screen);

        for (uint32 i=0; i<sprites.size(); ++i)
        {
            sprites[i]->update(event);
        }

        SDL_FreeSurface(screen);
        SDL_Flip(screen);
    }
}

DrawableObject.h:

#ifndef DRAWABLE_OBJECT_H
#define DRAWABLE_OBJECT_H

#include "SDL.h"
#include "SDL_Helpers.h"

class Game;

class DrawableObject
{
public:
    Game * const game;

    DrawableObject(Game * const game_);

    virtual void update(SDL_Event *event) = 0;

};

#endif

DrawableObject.cpp:

#include "DrawableObject.h"
#include "Game.h"

DrawableObject::DrawableObject(Game * const game_)
    :   game(game_)
{

}

Player.h:

#ifndef PLAYER_H
#define PLAYER_H

#include <cmath>
#include "DrawableObject.h"

class Player : public DrawableObject
{
public:
    Player(Game * const game);

    void update(SDL_Event *event);
};

#endif

Player.cpp:

#include "Player.h"
#include "Game.h"

Player::Player(Game * const game)
    : DrawableObject(game)
{

}

void Player::update(SDL_Event *event)
{
    draw_circle(game->screen, 100, 100, 50, 0xff000000);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨的味道风的声音 2024-10-03 18:46:47

您的编译行中缺少 Player.cpp。您遇到链接错误。

You're missing Player.cpp in your compilation line. You're having a link error.

不是循环依赖问题(至少不是您当前提出的问题)。您忘记编译Player.cpp

Not a circular-dependency issue (at least, not as you've currently framed your question). You forgot to compile Player.cpp.

烟酒忠诚 2024-10-03 18:46:47

供遇到此问题的其他人参考:如果您在 makefile 中的 G++ 命令中遗漏了输出文件,您也会收到相同的错误。

For reference to other people with this issue: You will also get the same error if you miss out a output file in your G++ command in a makefile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文