当实现完全在我的 .HPP 文件中时,编译器正在寻找 .CPP 文件

Compiler Looking for .CPP file when the Implementation is Entirely in My .HPP File

看起来我的编译器正在寻找与我的 rectangle.h 文件对应的 CPP 文件。我在 .h 文件中有我的整个实现,我不想制作一个 CPP 文件来对应。结果,我的编译器抛出以下错误:

/usr/bin/ld: /tmp/ccywfYT2.o: in function `Sprite::Sprite()':
main.cpp:(.text+0x8a2): undefined reference to `Rectangle::Rectangle()'
/usr/bin/ld: /tmp/ccywfYT2.o: in function 
`Sprite::Sprite(Graphics&, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > const&, int, int, int, int, float, float)':
main.cpp:(.text+0x919): undefined reference to `Rectangle::Rectangle()'
collect2: error: ld returned 1 exit status

例如,我的矩形 class 在 rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "globals.h"

class Rectangle {
    private:

        int _x, _y, _width, _height;

    public:

        Rectangle();
        Rectangle(int x, int y, int width, int height) : _x(x), _y(y), _width(width), _height(height) 
{};

        const int inline getCenterX() const {return this->_x + this->_width / 2;};
        const int inline getCenterY() const {return this->_y + this->_height / 2;};

        const int inline getLeft() const {return this->_x;};
        const int inline getRight() const {return this->_x + this->_width;};
        const int inline getTop() const {return this->_y;};
        const int inline getBottom() const {return this->_y + this->_height;};

        const int inline getWidth() const {return this->_width;};
        const int inline getHeight() const {return this->_height;};

        const int inline getSide(const ourSides::Side side) const {
                return  side == ourSides::TOP ? this->getBottom() : 
                        side == ourSides::BOTTOM ? this->getTop() : 
                        side == ourSides::LEFT ? this->getRight() :
                        side == ourSides::RIGHT ? this->getLeft() :
                        ourSides::NONE;
        }

        /* bool collidesWith
         * Takes in another rectangle and checks if the two are colliding.
         * Will be applied to Quote as he moves around. (Quote is in a rectangle)
         */ 

        const bool inline collidesWith(const Rectangle& otherRect) const {
            return 
                    this->getRight() >= otherRect.getLeft() &&
                    this->getLeft() <= otherRect.getRight() && 
                    this->getTop() <= otherRect.getBottom() &&
                    this->getBottom() >= otherRect.getTop();
        }

        const bool inline isValidRectangle() const {
            return this->_x >= 0 && this->_y >= 0 && this->_width >= 0 && this->_height >= 0;
        }

};

#endif // RECTANGLE_H

如何在不需要随附的 CPP 文件的情况下编译我的程序,如果这甚至是我的问题。

您实际上并没有实施 Rectangle()。你在它后面放一个 ; ,这使它成为一个声明。

如果您打算使构造函数为空,请改用 Rectangle() = default;——除非您使用的是一个非常老的编译器,出于某种原因不支持此功能。然后使用 {} 而不是 ;.