我有课Dimension
我在 Dimension.h 文件中定义了它(就像我的所有类一样):
class Dimension
{
public:
constexpr Dimension() noexcept;
constexpr Dimension(int w, int h) noexcept;
int width;
int height;
};
我想我可以像在我的所有课程中一样,将定义放在单独的 Dimension.cpp 中:
#include "Dimension.h"
constexpr Dimension::Dimension() noexcept : width(0), height(0) {}
constexpr Dimension::Dimension(int w, int h) noexcept : width(w), height(h) {}
但是当我尝试使用该类时,编译器告诉我:
警告:内联函数'constexpr Dimension::Dimension()
' 使用但从未定义
并在链接时:
未定义的引用 'pong::graphics::Dimension::Dimension()
'
(与其他构造函数相同)
如果我像这样在标题中定义类:
class Dimension
{
public:
constexpr Dimension() noexcept : width(0), height(0) {}
constexpr Dimension(int w, int h) noexcept : width(w), height(h) {}
int width;
int height;
};
并省略 .cpp 文件,一切正常。
我正在使用海湾合作委员会4.9.2。为什么单独定义不起作用?