- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 2 个项目的 visual studio 解决方案,其中一个是静态库。在这个库中,我有以下带有一些重载运算符的类(成员和非成员):
namespace Vec
{
class Vec2
{
...
inline Vec2& operator +=(const Vec2& b);
};
inline Vec2 operator+(const Vec2&a, const Vec2&b);
}
.cpp文件中的定义如下:
inline Vec2& Vec2::operator +=(const Vec2& b)
{
this->x += b.x;
this->y += b.y;
return *this;
}
inline Vec2 Vec::operator+(const Vec2& a, const Vec2& b)
{
return Vec2(a.x + b.x, a.y + b.y);
}
成员运算符的所有内容都可以正确编译和链接。非成员函数没有链接,错误如下:
Error 4 error LNK2019: unresolved external symbol "class Vec::Vec2 __cdecl Vec::operator+(class Vec::Vec2 const &,class Vec::Vec2 const &)" (??HVec@@YA?AVVec2@0@ABV10@0@Z) referenced in function _main
如果我将定义放在头文件中,或者将定义与声明分开但删除 inline 关键字,它会成功链接。
我做错了什么?
最佳答案
What am I doing wrong?
您的错误是从声明它的 header 中省略了 inline
函数的定义。如果您在不包含定义的 cpp 文件中使用该函数,您将违反语言规则。
§7.1.2/4(n3242 草案)
An inline function shall be defined in every translation unit in which it is odr-used...
如果您将函数声明为非内联,则上述规则不适用。如果您在 header 中包含定义,那么您就遵守了规则。
Proper way to split declaration and definition
您可以拆分它们,但不能拆分成单独的文件。
// in header
// declarations...
class Vec2
{
Vec2& operator +=(const Vec2& b);
};
Vec2 operator+(const Vec2&a, const Vec2&b);
// definitions...
inline Vec2& Vec2::operator +=(const Vec2& b)
{
...
}
inline Vec2 operator+(const Vec2& a, const Vec2& b)
{
...
}
关于c++ - 拆分内联非成员运算符的声明和定义的正确方法(链接器问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455820/
我是一名优秀的程序员,十分优秀!