首页 > 解决方案 > 未定义对“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();是游戏循环,因此不需要。

标签: c++

解决方案


推荐阅读