c++ - 未定义对“Base::Base(int, int)”的引用
问题描述
我正在制作一个小游戏,我想使用抽象类来轻松管理创建新实体。尽管我在创建构造函数时遇到了问题,但我将向您展示我的代码:
Base.h
#pragma once
#include <SFML/Graphics.hpp>
class Base{
public:
Base(int x, int y);
virtual ~Base() {}
virtual void tick() = 0;
virtual void render(sf::RenderWindow& g) = 0;
virtual void setPos(int x, int y) { this->x = x; this->y = y; }
virtual void setDimensions(int width, int height) { this->width = width; this->height = height; }
private:
int x, y, width, height = 0;
float velX, velY = 0;
};
Derive.h
#pragma once
#include <iostream>
#include "Base.h"
class Derive: public Base {
public:
Derive(int x, int y);
~Derive() {}
void tick() override;
void render(sf::RenderWindow& g) override;
private:
int x, y, width, height = 0;
float velX, velY = 0;
};
Derive.cpp
#include <iostream>
#include "headers/Derive.h"
Derive::Derive(int x, int y) : Base(x, y) {
this->x = x;
this->y = y;
this->width = 32;
this->height = 32;
this->velX = 0;
this->velY = 0;
}
void Derive::tick() {
}
void Derive::render(sf::RenderWindow& g) {
}
现在,我在Derive.cpp
文件中收到错误:
undefined reference to 'Base::Base(int, int)'
我一直在寻找很多时间,但找不到任何有用的东西,现在这可能是因为我最近才开始,但我特别决定学习游戏,因为我喜欢面向对象编程。
如果有什么我应该做的不同的事情,请告诉我,因为我最近开始学习 C++,我知道的越多越好。
有人要我的main
文件,但它被分开了,我只会显示相关代码:
Game.cpp
Game::Game() {
this->player = new Player(0,0);
}
Main.cpp
#include "headers/main.h"
#include "headers/game.h"
int Main::main() {
Game game;
game.run();
return 0;
}
指的game.run();
是游戏循环,因此不需要。
解决方案
推荐阅读
- node.js - 为 nodejs 安装 sqlite3
- java - 尝试在空对象引用上调用接口方法“ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)”
- android - 如何从 Firebase 获取特定的数据 uid/uuid
- azure - “ILoggingBuilder”不包含“AddNLog”的定义,并且没有扩展方法“AddNLog”接受“ILoggingBuilder”类型的第一个参数
- extjs - Ext js 6.6.0:Ext.Msg.alert 在日期字段更改侦听器中不起作用
- python - 使用中间件在每个视图之前调用的自定义装饰器
- android - 如何在 Android 上轻松调试 SOCKET 实现
- aem - 如何在 aem 6.5 触摸 UI 对话框中添加手风琴内容?
- singularity-container - 使用“shell”命令运行 Singularity 容器时自动源配置
- python-3.x - 我正在尝试使用来自 catvsdog 数据集的 tensorflow 网站的 mobileNet 实现迁移学习