C++ 未定义的引用
链接器错误:C++ 未定义的引用,c++,linker,undefined-reference,C++,Linker,Undefined Reference,链接器错误: $ 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/br
$ 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;
}
游戏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
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。你有一个链接错误 编译行中缺少Player.cpp。你有一个链接错误 这不是一个循环依赖性问题(至少不像您目前提出的问题那样)。您忘记编译
Player.cpp
不是一个循环依赖性问题(至少,不是您当前提出的问题)。您忘了编译Player.cpp
也许应该重命名该主题?这与循环依赖关系毫无关系。也许应该重命名主题?这与循环依赖关系无关。值得注意的是,循环依赖关系没有问题。忘记将其添加到我的makefile中了。谢谢。值得注意的是,循环依赖项没有问题。忘记将其添加到我的makefile中了。谢谢
#include "DrawableObject.h"
#include "Game.h"
DrawableObject::DrawableObject(Game * const game_)
: game(game_)
{
}
#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
#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);
}